// This is a comment.
A single-line comment. Dart also supports multi-line and document comments.
var
keyword.To create a variable use var
keyword. This keyword means that you can assign any type of value to the variable.
var name = "Santanu"; name = 30; // Error
Now after the literal gets assigned to the variable name
, the Dart automatically detect the type of the literal and decide how the variable should be treated throughout the execution of the program. In this case, the name is treated as String
type. If you try to assign any value which is not string, the Dart compiler throws an error and program execution halts.
dynamic
keywordThe dynamic
keyword is used to create a variable whose data type is unknown. You can use this keyword to store any kind of data anytime on the same variable. It is just like var
in Javascript.
dynamic value = 30; dynamic number = "Santanu Bera"; number = 30; // Works
Object
keywordIn dart everything is an Object. Even the keyword null
is also an object. The variable always store reference. The reference to the object which is assigned to the variable. As everything in Dart is an object you can object
keyword to create a variable. Which is same as dynamic
keyword. You can store any value anytime on the same variable.
Object name = "Santanu Bera"; name = 30; // Works
In dart everything is Object. Even number, string and null are object. That's why everything you store in a variable in an Object. Thus variable contains memory reference to the object which is assigned to the variable.
You can also use String
, bool
etc to create variable. These are called strong data types because you are declaring the type of data which is going to be assigned to the variable and the variable should not accept any value of other data type.
What if you declare a variable and leave it uninitialize with a value, the value null
is going to be the default value for any data type. So uninitialized or unassigned variable gets null
value by default.
int rollNumber; assert(rollNumber == null);
Use final
and const
keyword to declare a constant or variable whose value is fixed during the execution.
Instead of var
, use const
or final
to declare a constant.
const pi = 3.14; final numberOfStudents = 50;
If you want to explicitly specify the data type you can use the following structure:
const double pi = 3.14; final int numberOfStudents = 3.14;
The keyword const
takes compile time constant value or static constant. Whereas final
can take the runtime calculated or evaluated result of an expression. On the other hand, final
is used for dynamic constants. For example:
const int value1 = 70; // Works fine const int value1 = a + b + c; // Error, const takes a constant literal value not an expression. final int value2 = a + b + c; // Okay, final takes runtime calculated value. value2 = 60; // Error, Assigning other value is an error.
Once a value is set, further modification is not allowed.
Another difference is that final
is used to declare a class level constant. Whereas using const
for class level constant is an error. Instance variables can be final but not const.
Few example of static constants are:
const bar = 1000000; // Unit of pressure (dynes/cm2) const double atm = 1.01325 * bar; // Standard atmosphere