Variables and Datatypes in Dart
In Dart, variables are used to store data values. Each variable has a specific data type, which determines the kind of values it can hold. Dart is a statically typed language, which means that variables must be declared with their data types before they can be used.
Variable Declaration
In Dart, variables can be declared using the var
keyword followed by the variable name and an optional initial value. Here's an example:
var name = 'John';
In the above example, the variable name
is declared using the var
keyword. The variable name is name
and the initial value is 'John'
. The variable name
is of type String
because the initial value is a string.
The var
keyword is optional. You can also declare variables using the data type of the initial value. Here's an example:
String name = 'John';
In the above example, the variable name
is declared using the String
data type. The variable name is name
and the initial value is 'John'
. The variable name
is of type String
because the initial value is a string.
Variable Assignment
In Dart, variables can be assigned new values using the assignment operator =
. Here's an example:
var name = 'John';
name = 'Jane';
Output
Jane
In the above example, the variable name
is assigned the value 'John'
and then the value 'Jane'
.
Datatypes in Dart
In Dart, variables can be declared using the following data types:
int
- for integer valuesdouble
- for floating-point valuesnum
- for numeric valuesString
- for string valuesbool
- for boolean valuesList
- for list valuesMap
- for map valuesdynamic
- for dynamic values
We will discuss more about List
and Map
in the Dart collections chapter.
int
In Dart, the int
data type is used to represent integer values. Here's an example:
int age = 20;
In the above example, the variable age
is declared using the int
data type. The variable name is age
and the initial value is 20
. The variable age
is of type int
because the initial value is an integer.
double
In Dart, the double
data type is used to represent floating-point values. Here's an example:
double pi = 3.14;
In the above example, the variable pi
is declared using the double
data type. The variable name is pi
and the initial value is 3.14
. The variable pi
is of type double
because the initial value is a floating-point value.
num
In Dart, the num
data type is used to represent numeric values. Here's an example:
num pi = 3.14;
In the above example, the variable pi
is declared using the num
data type. The variable name is pi
and the initial value is 3.14
. The variable pi
is of type num
because the initial value is a numeric value.
String
In Dart, the String
data type is used to represent string values. Here's an example:
String name = 'John';
In the above example, the variable name
is declared using the String
data type. The variable name is name
and the initial value is 'John'
. The variable name
is of type String
because the initial value is a string value.
bool
In Dart, the bool
data type is used to represent boolean values. Here's an example:
bool isMarried = false;
In the above example, the variable isMarried
is declared using the bool
data type. The variable name is isMarried
and the initial value is false
. The variable isMarried
is of type bool
because the initial value is a boolean value.
dynamic
In Dart, the dynamic
data type is used to represent dynamic values. Here's an example:
dynamic age = 20;
In the above example, the variable age
is declared using the dynamic
data type. The variable name is age
and the initial value is 20
. The variable age
is of type dynamic
because the initial value is a dynamic value.
Type Inference
In Dart, the var
keyword can be used to declare variables without specifying their data types. Here's an example:
var name = 'John';
In the above example, the variable name
is declared using the var
keyword. The variable name is name
and the initial value is 'John'
. The variable name
is of type String
because the initial value is a string.
In Dart, the var
keyword can also be used to declare variables without specifying their data types. Here's an example:
var age = 20;
In the above example, the variable age
is declared using the var
keyword. The variable name is age
and the initial value is 20
. The variable age
is of type int
because the initial value is an integer.
Conclusion
In this chapter, we learned about variables and data types in Dart. In the next chapter, we will learn about keywords const
and final
in Dart.