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; 1 List y; 2 int x, y = 5, z; 3 def d; 4 int i = 10; 5 float[] f; 6 Map[][] m; 7
- declare
int x
; store defaultnull
tox
- declare
List y
; store defaultnull
toy
- declare
int x
; store defaultint 0
tox
; declareint y
; storeint 5
toy
; declareint z
; store defaultint 0
toz
; - declare
def d
; store defaultnull
tod
- declare
int i
; storeint 10
toi
- declare
float[] f
; store defaultnull
tof
- declare
Map[][] m
; store defaultnull
tom
- 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; 1 i = 10; 2
- declare
int i
; store defaultint 0
toi
- store
int 10
toi
- declare
Declaration combined with immediate assignment.
int i = 10; 1 double j = 2.0; 2
- declare
int i
; storeint 10
toi
- declare
double j
; storedouble 2.0
toj
- declare
Assignment of one variable to another using primitive type values.
int i = 10; 1 int j = i; 2
- declare
int i
; storeint 10
toi
- declare
int j
; load fromi
→int 10
; storeint 10
toj
- declare
Assignment with reference types using the new instance operator.
ArrayList l = new ArrayList(); 1 Map m = new HashMap(); 2
- declare
ArrayList l
; allocateArrayList
instance →ArrayList reference
; storeArrayList reference
tol
- declare
Map m
; allocateHashMap
instance →HashMap reference
; implicit castHashMap reference
toMap reference
→Map reference
; storeMap reference
tom
- declare
Assignment of one variable to another using reference type values.
List l = new ArrayList(); 1 List k = l; 2 List m; 3 m = k; 4
- declare
List l
; allocateArrayList
instance →ArrayList reference
; implicit castArrayList reference
toList reference
→List reference
; storeList reference
tol
- declare
List k
; load froml
→List reference
; storeList reference
tok
; (notel
andk
refer to the same instance known as a shallow-copy) - declare
List m
; store defaultnull
tom
- load from
k
→List reference
; storeList reference
tom
; (notel
,k
, andm
refer to the same instance)
- declare
Assignment with array type variables using the new array operator.
int[] ia1; 1 ia1 = new int[2]; 2 ia1[0] = 1; 3 int[] ib1 = ia1; 4 int[][] ic2 = new int[2][5]; 5 ic2[1][3] = 2; 6 ic2[0] = ia1; 7
- declare
int[] ia1
; store defaultnull
toia1
- allocate
1-d int array
instance withlength [2]
→1-d int array reference
; store1-d int array reference
toia1
- load from
ia1
→1-d int array reference
; storeint 1
toindex [0]
of1-d int array reference
- declare
int[] ib1
; load fromia1
→1-d int array reference
; store1-d int array reference
toib1
; (noteia1
andib1
refer to the same instance known as a shallow copy) - declare
int[][] ic2
; allocate2-d int array
instance withlength [2, 5]
→2-d int array reference
; store2-d int array reference
toic2
- load from
ic2
→2-d int array reference
; storeint 2
toindex [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 reference
toindex [0]
of2-d int array reference
; (noteia1
,ib1
, andindex [0]
ofia2
refer to the same instance)
- declare