Variables Example Program in Dart


This code is also written in the Dart programming language and defines a main() function that demonstrates some basic data types and variable declarations in Dart. Here's a breakdown of what's happening in this code:

  • var a=10;: This line declares a variable a and initializes it with a value of 10. The var keyword is used to declare variables with implicit type inference, meaning the data type of the variable is inferred by the Dart compiler based on the value it's initialized with.
  • print(a.runtimeType);: This line uses the print() function to output the data type of variable a to the console output. In Dart, the runtimeType property is used to determine the data type of a variable at runtime.
  • int b=25;: This line declares a variable b of type int and initializes it with a value of 25. Unlike the var keyword, the int keyword explicitly declares the data type of the variable as an integer.
  • print('The Value of b is ${b}');: This line uses string interpolation to output the value of variable b to the console output as part of a string.
  • double x=25.3;: This line declares a variable x of type double and initializes it with a value of 25.3. Like with int, the double keyword is used to explicitly declare the data type of the variable as a floating-point number.
  • print(x.runtimeType);: This line outputs the data type of variable x to the console output using print() and the runtimeType property.
  • String y='Joes';: This line declares a variable y of type String and initializes it with a string value of "Joes". The String keyword is used to explicitly declare the data type of the variable as a string.
  • print(y.runtimeType);: This line outputs the data type of variable y to the console output using print() and the runtimeType property.
  • dynamic z;: This line declares a variable z without an initial value and with the data type dynamic. dynamic is a special data type in Dart that allows the value of the variable to change at runtime.
  • z=10;: This line assigns a value of 10 to the variable z. Because z is of type dynamic, it can hold any type of value, including integers.
  • print('The Value of Z ${z} type : ${z.runtimeType}'); : This line outputs the value of variable z to the console output using string interpolation, as well as its data type using the runtimeType property. Because z was assigned a value of 10 in the previous line, its data type and value will be output as "int" and "10", respectively.
  • z=25.5;: This line assigns a value of 25.5 to the variable z. Because z is of type dynamic, it can now hold floating-point numbers in addition to integers.
  • print('The Value of Z ${z} type : ${z.runtimeType}'); : This line outputs the new value of variable z and its data type to the console output using string interpolation. Because z was assigned a value of 25.5 in the previous line, its data type and value will be output as "double" and "25.5", respectively.

Source Code

void main()
{
   var a=10;
   print(a.runtimeType);
   int b=25;
   print('The Value of b is ${b}');
   double x=25.3;
   print(x.runtimeType);
   String y='Joes';
   print(y.runtimeType);
   dynamic z;
   z=10;
   print('The Value of Z ${z} type : ${z.runtimeType}');
   z=25.5;
   print('The Value of Z ${z} type : ${z.runtimeType}');

}
To download raw file Click Here

Output

int
The Value of b is 25
double
String
The Value of Z 10 type : int
The Value of Z 25.5 type : doubl