Variables in Dart Programming Language

Variables are name given to memory locations or references which contains some type of object whose content may vary during the execution of program.

In programming, Variables are those entity whose value can be changed during program execution. In dart, variable stores references or address of memory location which stores some object. For example:

Examples


int number = 67;
String name="Alex Way";
var rate = 1.84;

Here number, rate & name are variables.

The variable called number contains a reference to a int object with a value of 67. Here int represents number is of type integer.

Similarly, the variable called name contains a reference to a String object with value of "Alex Way". Here String represents name is of type string and strings are collection of characters.

The variable called rate contains a reference to 1.84 object and type of this object at run time will be double. In this case type is not mentioned directly but it will be resolved by looking at value.

Syntax for Declaring Variables

Dart uses following syntax for creating variables:


data_type   variable_name;
or
data_type   variable_name = some_value;

Here, data_type can be any valid dart data types including var, const & final, and variable_name can be any valid dart identifiers.

Default Value of Variables

In dart, default value of variable is null. Which can be verified with the help of following program:


void main() {
  int number;
  String name;
  
  print('Default value of number is $number.');
  print('Default value of name is $name.');
}

Output

Default value of number is null.
Default value of name is null.