Variablesedit

A variable loads and stores a value for evaluation during operations.

Declarationedit

Declare a variable before use with the format of type followed by identifier. Declare an array type variable using an opening [ token and a closing ] token for each dimension directly after the identifier. Specify a comma-separated list of identifiers following the type to declare multiple variables in a single statement. Use an assignment operator combined with a declaration to immediately assign a value to a variable. A variable not immediately assigned a value will have a default value assigned implicitly based on the type.

Errors

  • If a variable is used prior to or without declaration.

Grammar

declaration : type ID assignment? (',' ID assignment?)*;
type: ID ('.' ID)* ('[' ']')*;
assignment: '=' expression;

Examples

  • Different variations of variable declaration.

     int x;
     List y;
     int x, y = 5, z;
     def d;
     int i = 10;
     float[] f;
     Map[][] m;

    declare int x; store default null to x

    declare List y; store default null to y

    declare int x; store default int 0 to x; declare int y; store int 5 to y; declare int z; store default int 0 to z;

    declare def d; store default null to d

    declare int i; store int 10 to i

    declare float[] f; store default null to f

    declare Map[][] m; store default null to m

Assignmentedit

Use the assignment operator '=' to store a value in a variable for use in subsequent operations. Any operation that produces a value can be assigned to any variable as long as the types are the same or the resultant type can be implicitly cast to the variable type.

Errors

  • If the type of value is unable to match the type of variable.

Grammar

assignment: ID '=' expression

Examples

  • Variable assignment with an integer literal.

     int i;
     i = 10;

    declare int i; store default int 0 to i

    store int 10 to i

  • Declaration combined with immediate assignment.

     int i = 10;
     double j = 2.0;

    declare int i; store int 10 to i

    declare double j; store double 2.0 to j

  • Assignment of one variable to another using primitive type values.

     int i = 10;
     int j = i;

    declare int i; store int 10 to i

    declare int j; load from iint 10; store int 10 to j

  • Assignment with reference types using the new instance operator.

     ArrayList l = new ArrayList();
     Map m = new HashMap();

    declare ArrayList l; allocate ArrayList instance → ArrayList reference; store ArrayList reference to l

    declare Map m; allocate HashMap instance → HashMap reference; implicit cast HashMap reference to Map referenceMap reference; store Map reference to m

  • Assignment of one variable to another using reference type values.

     List l = new ArrayList();
     List k = l;
     List m;
     m = k;

    declare List l; allocate ArrayList instance → ArrayList reference; implicit cast ArrayList reference to List referenceList reference; store List reference to l

    declare List k; load from lList reference; store List reference to k; (note l and k refer to the same instance known as a shallow-copy)

    declare List m; store default null to m

    load from kList reference; store List reference to m; (note l, k, and m refer to the same instance)

  • Assignment with array type variables using the new array operator.

     int[] ia1;
     ia1 = new int[2];
     ia1[0] = 1;
     int[] ib1 = ia1;
     int[][] ic2 = new int[2][5];
     ic2[1][3] = 2;
     ic2[0] = ia1;

    declare int[] ia1; store default null to ia1

    allocate 1-d int array instance with length [2]1-d int array reference; store 1-d int array reference to ia1

    load from ia11-d int array reference; store int 1 to index [0] of 1-d int array reference

    declare int[] ib1; load from ia11-d int array reference; store 1-d int array reference to ib1; (note ia1 and ib1 refer to the same instance known as a shallow copy)

    declare int[][] ic2; allocate 2-d int array instance with length [2, 5]2-d int array reference; store 2-d int array reference to ic2

    load from ic22-d int array reference; store int 2 to index [1, 3] of 2-d int array reference

    load from ia11-d int array reference; load from ic22-d int array reference; store 1-d int array reference to index [0] of 2-d int array reference; (note ia1, ib1, and index [0] of ia2 refer to the same instance)