Dart – Variables & Constants
A variable is an abstract storage location paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value; or in simpler terms, a variable is a container for a particular set of bits or type of data (like integer, float, String etc…). A variable can eventually be associated with or identified by a memory address.Wikipedia
In simple words, Variable are like storage box that holds some data or value and this data/value can be used or changed during the execution of the program.
In Dart, a variable must be declared before they are used. Variables can be declared by
var keyword followed by the variable name or by type annotation while declaring a variable to suggest a type of the value variable can hold.
var <variable_name> = value;
var pi = 3.14;
This statement means we’re declaring some space for a variable called
pi. and assign a value –
3.14 Note that the semicolon at the end of the line
; is how your compiler separates one program statement from another.
Dart is a type inferred language, which allows the compiler automatically infers (know) the type of data we want to store based on the initial value we assign.
Although Dart is a type inferred language, you can optionally provide a type annotation while declaring a variable to suggest a type of the value variable can hold. In Dart, prefixing the variable name with the data type ensures that a variable holds only data specific to a data type.
<type> <variable_name> = value;
double pi = 3.14;
Here is an example of declaring an integer, which we’ve called
pi and assign a value –
3.14. This statement means we’re declaring some space for a variable called
pi, which will be used to store double data.
Dart Constants refers to an immutable value. Constants are basically literals whose values cannot be modified or changed during the execution of the program. Constant must be initialized when declared as values cannot be assigned it later.
Defining Constants in Dart
Dart, constants can be created in the following two ways –
- Using final keyword.
- Using const keyword.
final variable can be set only once while
const keyword represents a compile-time constant. The constant declared with
const keyword are implicitly final.
Defining Constant with final Keyword
final <constant_name> = value;
final <type> <constant_name> = value;
final pi = 3.14; final String = "You Cant Change me!"
Defining Constant with const Keyword
const <constant_name> = value; const <type> <contanst_name> = value;
const pi = 3.14;
Rules for writing Variable name:
- The variable names can consist of letters and alphabets.
- Keywords are not allowed to use as a variable name.
- Blank spaces are not allowed in variable names.
- The first character of a variable should always be an alphabet and cannot be a digit.
- Variable names are case sensitive i.e. UPPER and lower case are significant.
- Special characters like #, $ are not allowed except for the underscore (_) and the dollar ($) sign.
In the next lesson, you will learn about Input&Output.