Keywords are pre-defined reserved words which have a special meaning to the interpreted compiler.

There are 61 keywords available in dart language. Some of they are – if, else, while, for, extends, abstract etc.

Data types

Data types used in dart language refer to an extensive system that we use to declare various types of functions or variables in a program. Here, on the basis of the type of variable present in a program, we determine the space that it occupies in storage, along with the way in which the stored bit pattern will be interpreted.

A data type specifies the type of data that a variable can store.

Examples of static data types in dart – Numbers (integer, double), Strings, Boolean etc.

Numbers

Dart numbers come in two flavours: int and double

Integer(int)

Integer values no larger than 64 bits, depending on the platform. On native platforms, values can be from -2^63 to 2^63 – 1. On the web, integer values are represented as JavaScript numbers (64-bit floating-point values with no fractional part) and can be from -2^53 to 2^53 – 1.

double

64-bit (double-precision) floating-point numbers, as specified by the IEEE 754 standard. Both int and double are subtypes of num. The num type includes basic operators such as +, -, /, and *, and is also where you’ll find abs(), ceil(), and floor(), among other methods. (Bitwise operators, such as >>, are defined in the int class.) If num and its subtypes don’t have what you’re looking for, the dart:math library might.

Code 1:

void main(){
  // integer variable ‘a’ which initialized with value 10
  int a=10;
  print(a);
  print(a.runtimeType);
  // integer variable ‘b’ which initialized with value 20.3
  double b=20.3;
  print(b);
  print(b.runtimeType);
}
output

Typecasting

Type casting is a way of converting data from one data type to another data type. This process of data conversion is also known as type conversion or type coercion.

Let’s see some examples of change variables data type into another data types.

Note: Dart Object type has a .runtimeType instance member (source is from dart-sdk v1.14, don’t know if it was available earlier) for checking any variables data type during run, you need to use .runtimeType

You can also declare a variable as a num. If you do this, the variable can contains both integer and double values. Let’s see below code…

Code 2:

void main() {
  num a = 2;
  print(‘a is $a’);
  print(a.runtimeType);
  a = 90.8;
  print(‘a is $a’);
  print(a.runtimeType);
}
output

** remember: from the upper code, you see variable ‘a’ data type get changed from int->double.

Now let’s converts a string into integer [typecasting another example]

Code 3:    .parse() function helps to convert String to int/double.

void main() {
  // string ’23’ will converted into integer 23 using .parse() function
  int a = int.parse(’23’);
  print(‘a is $a’);
  print(‘variable a type is ‘);
  print(a.runtimeType);
}
output

Let’s see some another example of typecasting, string -> integer

Code 4:

void main() {
  String age = ’23’;
  print(‘age is $age’);
  print(‘age variable type ‘);
  print(age.runtimeType);
 
  // variable age inserted into function .parse()
  int a = int.parse(age);
  print(‘a is $a’);
  print(‘variable a type is ‘);
  print(a.runtimeType);
}
output

Now let’s converts a string into double (string->double)

Code 5:

void main(){
  String val=’20.3′;
  print(‘String val is: $val’);
  print(‘val variable data-type: ‘);
  print(val.runtimeType);
 
  // typecasting string->double
  double amnt=double.parse(val);
  print(‘amnt is $amnt’);
  print(‘amnt variable data-type: ‘);
  print(amnt.runtimeType);
}
output

Now, let’s convert a integers/double valued variables into strings.

Note: for this we need to use .toString() function/method.

Code 6:

void main(){
  int a=10;
  print(‘a is: $a’);
  print(a.runtimeType);
  // typecasting int->string by .toString()
  String b=a.toString();
  print(‘b is: $b’);
  print(b.runtimeType);
}
output

Code 7: converts a double into string

void main(){
  double a=23.45;
  print(‘a is: $a’);
  print(a.runtimeType);
 
  // double->string .toString()
  String df=a.toString();
  print(‘String df is: $df’);
  print(df.runtimeType);
}
output

Write a code, to print every variable with their respective data type as integer, double, String, bool.

Code 8:

void main(){
  // integer variable ‘a’ with stored value ’10’
  int a=10;
  print(‘a is: $a’);
  print(‘variable A data type: ‘);
  print(a.runtimeType);
  // double variable ‘b’ with stored value ‘10.1’
  double b=10.1;
  print(‘b is: $b’);
  print(‘variable B data type: ‘);
  print(b.runtimeType);
  // String variable ‘c’ with stored value ‘microcodes’
  String c=”microcodes”;
  print(‘c is: $c’);
  print(‘variable C data type: ‘);
  print(c.runtimeType);
  // boolean variable ‘d’ with stored value ‘true’
  bool d=true;
  print(‘d is: $d’);
  d=false;
  print(‘d is: $d’);
  print(‘variable D data type: ‘);
  print(d.runtimeType);
}
output