Dart – Variables and Constants

1 0
Read Time:2 Minute, 53 Second

Dart – Variables & Constants

Variables

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.

Declaring Variables

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 Keyword:

Syntax:

var <variable_name> = value;

Example:

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.

Type Annotations

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.

Syntax:

<type> <variable_name> = value;

Example:

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.

Constants

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.

A 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

Syntax:

final <constant_name> = value;

Or

final <type> <constant_name> = value;

Example:

final pi = 3.14;
final String = "You Cant Change me!"

Defining Constant with const Keyword

Syntax:

const <constant_name> = value;
const <type> <contanst_name> = value;

Example:

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.

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

2 thoughts on “Dart – Variables and Constants

Leave a Comment

Verified by MonsterInsights