Variables

  1. Definition:
  2. To store a value in the computer memory we need an identifier (variable name) that refers to the memory location of this value. The value can be changed during the execution of the program.


  3. Naming Rules:
    • Contain alphabets & numbers.

    • Spaces, special character (except the underscore _) and the dollar sign ($) are not allowed

    • Cannot begin with a number

    • Cannot be Reserved Keywords

    • Reserved Keywords : are the words predefined by the compiler like: for, get, null, true, void…


  4. Syntax:
  5. Before using the variable, it’s necessary to declare it:

    var <variableName>;

    var country;

    we can initial it with a value or it will take the default value “null”:

    var <variableName> = <expression>;

    var country = ”Japan”;

    Dart detect the variable type with the initial value.
    We can optionally provide a type to ensures the data types of the variable:

    <type> <variableName>;

    String country;

    <type> <variableName> = <expression>;

    String country = ”Japan”;

    For multiple variables declarations:

    <type> varA, varB, varC ,.. ;

    int A,B,C;

    Note: ( = ) is called the assignment operator that gives to the variable in the left the value of the expression in the right.


  6. Types:


  7. Type Example
    Integer int count = 32;
    Double double average = 3.14;
    String String name = ”john”;
    Boolean bool isReady = true;
    List List nums = [13, 7, 34];
    Map Map couple = {“husband”:”Mario”, ”wife”:”Diana”}
    Runes var heart = '\u2665';