Add Your Heading Text Here

Variables are one of the most important concepts in the programming languages.

They are named memory spaces or temporary assignments for storing values in the program.

The name provided to the variable is known as identifiers.

For ex :

int A=5;

Here, A is a variable storing the reference to the int object with value 5. and A is also the identifier name provided to the variable to be recognized.

Variable Naming Convention Rules

We can assign any name to our variables following some predefined naming convention rules as below,

  • Variable names can contain Alphabets and Numbers.
  • Variable names cannot start with Numbers.
  • They cannot contain spaces and special Characters except dollar ($) and underscore (_) sign.
  • They cannot be the same as reserved keywords like if, for, switch, etc.

Why do we have to follow the naming convention rules?

As we all know that compiler or interpreter can compute or manipulate data much faster than humans. but, they don’t have the capability to think on their own. so, if you assign your variable name as reserved keyword like,

For ex:

int for=0;

So, the compiler will not be able to differentiate between the variable name for and for loop. So to resolve the confliction/ambiguity there are certain rules predefined for naming an identifier.

So, here is the list of predefined keywords which cannot be used as variable names are as following

abstractelseimportsuper
asenuminswitch
assertexportinterfacesync
asyncextendsisthis
awaitextensionlibrarythrow
breakexternalmixintrue
casefactorynewtry
catchfalsenulltypedef
classfinalonvar
constfinallyoperatorvoid
continueforpartwhile
covariantFunctionrethrowwith
defaultgetreturnyield
deferredhidesetdo
ifshowdynamicimplements
static

Variable Syntax

A variable must be declared before it is used in Program.

var the keyword is used for declaring the variable data type dynamically in dart or you can also declare specific data type before the variable name to assign the value.

For ex :

var variableName=”Shiva”;

Var is the best way to declare a variable without specifying its data type.

All variables in dart stores references to the objects or values.

For ex :

int number=10;

Here variable stores the reference(address in memory) to the identifier number containing value 10.

Most of the Languages Supports Type Checking by prefixing the data type with variable name. Type Checking ensures that variables declared with specific data type stores only the values to that specific data type.

For ex :

String name=”Billy”;

Here variable name can contain only String data type values. user cannot assign int or boolean value to String data type variables.

So by prefixing var keyword user do not have to define the data type for the variable. variable will predict the data type which type of data is assigned to variable.

For ex :

var x = 10;

Now x will act as the Int data type and it will have all the properties and methods of Int data type. var y=”hi” similarly, here y will act as the String data type and it will have all the properties and methods of String data type.

So var keyword is used when you do not know which kind of data will be stored in the data type. So at the time of variable declaration using var keyword will help you to get dynamic data type at runtime.

Initial Values

Variables declared but not initialized in dart have initial values null.

For ex :

void main()
{
	var a;
	int b;
	boolean c;
	print(a);
	print(b);
	print(c);
}

Output :

null
null
null

Final Variable

Final variable is the variables which should be initialized at the time of variable declaration.

The value of the final variable can be set only once. we cannot change the value of the final variable at any time.

The final variable doesn’t have any default value. So, the final variable must be Initialized.

When you use a final variable you do not need to define the data type of the variable. final will act as the data type which is assigned to variable.

For ex :

void main()
{
  final value="Hi";
  final value2=101;
  print(value);
  print(value2);
}

Output :

Hi
101

Const Variable

Constant variable is used to store the constant value in the variable.

It must be initialised at the time of declaration.

Also the value of the const variable can be set only once. we can’t change the value of const variable at any time.

The const variable doesn’t have any default value. so, the const variable must be Initialised.

When you use const variable you do not need to define the data type of the variable. const will act as the data type which is assigned to variable.

For ex :

void main()
{
  const value="Hello";
  const value2=102;
  print(value);
  print(value2);
}

Output :

Hello
102

The only difference between final and const is that we can assign the final variable any other value while const variable can have only constant variable. You cannot assign any other value to it.

For ex :

void main()
{
  for(int i=0;i<10;i++)
  {
    final value=i;
    print(value);
  }
}

Output :

0123456789

For ex :

void main()
{
     final i=0;
     const value=i;
     print(value);
}

It will give you a compile time error. because constant can have only compile time values (i.e., values assigned at the time of variable declaration). You cannot assign any value that can change at run time.

So these are the different usages of variables.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Show Buttons
Hide Buttons