NOTE: You are looking at documentation for an older release. For the latest information, see the current release documentation.
Variables
editVariables
editA variable loads and stores a value for evaluation during operations.
Declaration
editDeclare 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.
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
Assignment
editUse 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.
-
Declaration combined with immediate assignment.
-
Assignment of one variable to another using primitive type values.
-
Assignment with reference types using the new instance operator.
-
Assignment of one variable to another using reference type values.
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) -
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 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)