Variables
A variable loads and stores a value for evaluation during operations.
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 defaultnulltox - declare
List y; store defaultnulltoy - declare
int x; store defaultint 0tox; declareint y; storeint 5toy; declareint z; store defaultint 0toz; - declare
def d; store defaultnulltod - declare
int i; storeint 10toi - declare
float[] f; store defaultnulltof - declare
Map[][] m; store defaultnulltom
- declare
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 defaultint 0toi - store
int 10toi
- declare
Declaration combined with immediate assignment.
int i = 10; double j = 2.0;- declare
int i; storeint 10toi - declare
double j; storedouble 2.0toj
- declare
Assignment of one variable to another using primitive type values.
int i = 10; int j = i;- declare
int i; storeint 10toi - declare
int j; load fromi→int 10; storeint 10toj
- declare
Assignment with reference types using the new instance operator.
ArrayList l = new ArrayList(); Map m = new HashMap();- declare
ArrayList l; allocateArrayListinstance →ArrayList reference; storeArrayList referencetol - declare
Map m; allocateHashMapinstance →HashMap reference; implicit castHashMap referencetoMap reference→Map reference; storeMap referencetom
- declare
Assignment of one variable to another using reference type values.
List l = new ArrayList(); List k = l; List m; m = k;- declare
List l; allocateArrayListinstance →ArrayList reference; implicit castArrayList referencetoList reference→List reference; storeList referencetol - declare
List k; load froml→List reference; storeList referencetok; (notelandkrefer to the same instance known as a shallow-copy) - declare
List m; store defaultnulltom - load from
k→List reference; storeList referencetom; (notel,k, andmrefer to the same instance)
- declare
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 defaultnulltoia1 - allocate
1-d int arrayinstance withlength [2]→1-d int array reference; store1-d int array referencetoia1 - load from
ia1→1-d int array reference; storeint 1toindex [0]of1-d int array reference - declare
int[] ib1; load fromia1→1-d int array reference; store1-d int array referencetoib1; (noteia1andib1refer to the same instance known as a shallow copy) - declare
int[][] ic2; allocate2-d int arrayinstance withlength [2, 5]→2-d int array reference; store2-d int array referencetoic2 - load from
ic2→2-d int array reference; storeint 2toindex [1, 3]of2-d int array reference - load from
ia1→1-d int array reference; load fromic2→2-d int array reference; store1-d int array referencetoindex [0]of2-d int array reference; (noteia1,ib1, andindex [0]ofia2refer to the same instance)
- declare