Operators: Reference
Use the method call operator '()' to call a member method on a reference type value. Implicit boxing/unboxing is evaluated as necessary per argument during the method call. When a method call is made on a target def type value, the parameters and return type value are considered to also be of the def type and are evaluated at run-time.
An overloaded method is one that shares the same name with two or more methods. A method is overloaded based on arity where the same name is re-used for multiple methods as long as the number of parameters differs.
Errors
- If the reference type value is
null. - If the member method name doesn’t exist for a given reference type value.
- If the number of arguments passed in is different from the number of specified parameters.
- If the arguments cannot be implicitly cast or implicitly boxed/unboxed to the correct type values for the parameters.
Grammar
method_call: '.' ID arguments;
arguments: '(' (expression (',' expression)*)? ')';
Examples
Method calls on different reference types.
Map m = new HashMap(); m.put(1, 2); int z = m.get(1); def d = new ArrayList(); d.add(1); int i = Integer.parseInt(d.get(0).toString());- declare
Map m; allocateHashMapinstance →HashMap reference; storeHashMap referencetom - load from
m→Map reference; implicit castint 1todef→def; implicit castint 2todef→def; callputonMap referencewith arguments (int 1,int 2) - declare
int z; load fromm→Map reference; callgetonMap referencewith arguments (int 1) →def; implicit castdeftoint 2→int 2; storeint 2toz - declare
def d; allocateArrayListinstance →ArrayList reference; implicit castArrayListtodef→def; storedeftod - load from
d→def; implicit castdeftoArrayList reference→ArrayList referencecalladdonArrayList referencewith arguments (int 1); - declare
int i; load fromd→def; implicit castdeftoArrayList reference→ArrayList referencecallgetonArrayList referencewith arguments (int 1) →def; implicit castdeftoInteger 1 reference→Integer 1 reference; calltoStringonInteger 1 reference→String '1'; callparseIntonIntegerwith arguments (String '1') →int 1; storeint 1ini;
- declare
Use the field access operator '.' to store a value to or load a value from a reference type member field.
Errors
- If the reference type value is
null. - If the member field name doesn’t exist for a given reference type value.
Grammar
field_access: '.' ID;
Examples
The examples use the following reference type definition:
name:
Example
non-static member fields:
* int x
* def y
* List z
Field access with the
Exampletype.Example example = new Example(); example.x = 1; example.y = example.x; example.z = new ArrayList(); example.z.add(1); example.x = example.z.get(0);- declare
Example example; allocateExampleinstance →Example reference; storeExample referencetoexample - load from
example→Example reference; storeint 1toxofExample reference - load from
example→Example reference @0; load fromexample→Example reference @1; load fromxofExample reference @1→int 1; implicit castint 1todef→def; storedeftoyofExample reference @0; (noteExample reference @0andExample reference @1are the same) - load from
example→Example reference; allocateArrayListinstance →ArrayList reference; implicit castArrayList referencetoList reference→List reference; storeList referencetozofExample reference - load from
example→Example reference; load fromzofExample reference→List reference; calladdonList referencewith arguments (int 1) - load from
example→Example reference @0; load fromexample→Example reference @1; load fromzofExample reference @1→List reference; callgetonList referencewith arguments (int 0) →int 1; storeint 1inxofList reference @0; (noteExample reference @0andExample reference @1are the same)
- declare
Use the null safe operator '?.' instead of the method call operator or field access operator to ensure a reference type value is non-null before a method call or field access. A null value will be returned if the reference type value is null, otherwise the method call or field access is evaluated.
Errors
- If the method call return type value or the field access type value is not a reference type value and is not implicitly castable to a reference type value.
Grammar
null_safe: null_safe_method_call
| null_safe_field_access
;
null_safe_method_call: '?.' ID arguments;
arguments: '(' (expression (',' expression)*)? ')';
null_safe_field_access: '?.' ID;
Examples
The examples use the following reference type definition:
name:
Example
non-static member methods:
* List factory()
non-static member fields:
* List x
Null safe without a
nullvalue.Example example = new Example(); List x = example?.factory();- declare
Example example; allocateExampleinstance →Example reference; storeExample referencetoexample - declare
List x; load fromexample→Example reference; null safe callfactoryonExample reference→List reference; storeList referencetox;
- declare
Null safe with a
nullvalue;Example example = null; List x = example?.x;- declare
Example example; storenulltoexample - declare
List x; load fromexample→Example reference; null safe accessxonExample reference→null; storenulltox; (note the null safe operator returnednullbecauseexampleisnull)
- declare
Use the list initialization operator '[]' to allocate an List type instance to the heap with a set of pre-defined values. Each value used to initialize the List type instance is cast to a def type value upon insertion into the List type instance using the add method. The order of the specified values is maintained.
Grammar
list_initialization: '[' expression (',' expression)* ']'
| '[' ']';
Examples
List initialization of an empty
Listtype value.List empty = [];- declare
List empty; allocateArrayListinstance →ArrayList reference; implicit castArrayList referencetoList reference→List reference; storeList referencetoempty
- declare
List initialization with static values.
List list = [1, 2, 3];- declare
List list; allocateArrayListinstance →ArrayList reference; calladdonArrayList referencewith arguments(int 1); calladdonArrayList referencewith arguments(int 2); calladdonArrayList referencewith arguments(int 3); implicit castArrayList referencetoList reference→List reference; storeList referencetolist
- declare
List initialization with non-static values.
int i = 1; long l = 2L; float f = 3.0F; double d = 4.0; String s = "5"; List list = [i, l, f*d, s];- declare
int i; storeint 1toi - declare
long l; storelong 2tol - declare
float f; storefloat 3.0tof - declare
double d; storedouble 4.0tod - declare
String s; storeString "5"tos - declare
List list; allocateArrayListinstance →ArrayList reference; load fromi→int 1; calladdonArrayList referencewith arguments(int 1); load froml→long 2; calladdonArrayList referencewith arguments(long 2); load fromf→float 3.0; load fromd→double 4.0; promotefloat 3.0anddouble 4.0: resultdouble; implicit castfloat 3.0todouble 3.0→double 3.0; multiplydouble 3.0anddouble 4.0→double 12.0; calladdonArrayList referencewith arguments(double 12.0); load froms→String "5"; calladdonArrayList referencewith arguments(String "5"); implicit castArrayList referencetoList reference→List reference; storeList referencetolist
- declare
Use the list access operator '[]' as a shortcut for a set method call or get method call made on a List type value.
Errors
- If a value other than a
Listtype value is accessed. - If a non-integer type value is used as an index for a
setmethod call orgetmethod call.
Grammar
list_access: '[' expression ']'
Examples
List access with the
Listtype.List list = new ArrayList(); list.add(1); list.add(2); list.add(3); list[0] = 2; list[1] = 5; int x = list[0] + list[1]; int y = 1; int z = list[y];- declare
List list; allocateArrayListinstance →ArrayList reference; implicit castArrayList referencetoList reference→List reference; storeList referencetolist - load from
list→List reference; calladdonList referencewith arguments(int 1) - load from
list→List reference; calladdonList referencewith arguments(int 2) - load from
list→List reference; calladdonList referencewith arguments(int 3) - load from
list→List reference; callsetonList referencewith arguments(int 0,int 2) - load from
list→List reference; callsetonList referencewith arguments(int 1,int 5) - declare
int x; load fromlist→List reference; callgetonList referencewith arguments(int 0) →def; implicit castdeftoint 2→int 2; load fromlist→List reference; callgetonList referencewith arguments(int 1) →def; implicit castdeftoint 5→int 5; addint 2andint 5→int 7; storeint 7tox - declare
int y; storeint 1inty - declare
int z; load fromlist→List reference; load fromy→int 1; callgetonList referencewith arguments(int 1) →def; implicit castdeftoint 5→int 5; storeint 5toz
- declare
List access with the
deftype.def d = new ArrayList(); d.add(1); d.add(2); d.add(3); d[0] = 2; d[1] = 5; def x = d[0] + d[1]; def y = 1; def z = d[y];- declare
List d; allocateArrayListinstance →ArrayList reference; implicit castArrayList referencetodef→def; storedeftod - load from
d→def; implicit castdeftoArrayList reference→ArrayList reference; calladdonArrayList referencewith arguments(int 1) - load from
d→def; implicit castdeftoArrayList reference→ArrayList reference; calladdonArrayList referencewith arguments(int 2) - load from
d→def; implicit castdeftoArrayList reference→ArrayList reference; calladdonArrayList referencewith arguments(int 3) - load from
d→def; implicit castdeftoArrayList reference→ArrayList reference; callsetonArrayList referencewith arguments(int 0,int 2) - load from
d→def; implicit castdeftoArrayList reference→ArrayList reference; callsetonArrayList referencewith arguments(int 1,int 5) - declare
def x; load fromd→def; implicit castdeftoArrayList reference→ArrayList reference; callgetonArrayList referencewith arguments(int 0) →def; implicit castdeftoint 2→int 2; load fromd→def; implicit castdeftoArrayList reference→ArrayList reference; callgetonArrayList referencewith arguments(int 1) →def; implicit castdeftoint 2→int 2; addint 2andint 5→int 7; storeint 7tox - declare
int y; storeint 1inty - declare
int z; load fromd→ArrayList reference; load fromy→def; implicit castdeftoint 1→int 1; callgetonArrayList referencewith arguments(int 1) →def; storedeftoz
- declare
Use the map initialization operator '[:]' to allocate a Map type instance to the heap with a set of pre-defined values. Each pair of values used to initialize the Map type instance are cast to def type values upon insertion into the Map type instance using the put method.
Grammar
map_initialization: '[' key_pair (',' key_pair)* ']'
| '[' ':' ']';
key_pair: expression ':' expression
Examples
Map initialization of an empty
Maptype value.Map empty = [:];- declare
Map empty; allocateHashMapinstance →HashMap reference; implicit castHashMap referencetoMap reference→Map reference; storeMap referencetoempty
- declare
Map initialization with static values.
Map map = [1:2, 3:4, 5:6];- declare
Map map; allocateHashMapinstance →HashMap reference; callputonHashMap referencewith arguments(int 1,int 2); callputonHashMap referencewith arguments(int 3,int 4); callputonHashMap referencewith arguments(int 5,int 6); implicit castHashMap referencetoMap reference→Map reference; storeMap referencetomap
- declare
Map initialization with non-static values.
byte b = 0; int i = 1; long l = 2L; float f = 3.0F; double d = 4.0; String s = "5"; Map map = [b:i, l:f*d, d:s];- declare
byte b; storebyte 0tob - declare
int i; storeint 1toi - declare
long l; storelong 2tol - declare
float f; storefloat 3.0tof - declare
double d; storedouble 4.0tod - declare
String s; storeString "5"tos - declare
Map map; allocateHashMapinstance →HashMap reference; load fromb→byte 0; load fromi→int 1; callputonHashMap referencewith arguments(byte 0,int 1); load froml→long 2; load fromf→float 3.0; load fromd→double 4.0; promotefloat 3.0anddouble 4.0: resultdouble; implicit castfloat 3.0todouble 3.0→double 3.0; multiplydouble 3.0anddouble 4.0→double 12.0; callputonHashMap referencewith arguments(long 2,double 12.0); load fromd→double 4.0; load froms→String "5"; callputonHashMap referencewith arguments(double 4.0,String "5"); implicit castHashMap referencetoMap reference→Map reference; storeMap referencetomap
- declare
Use the map access operator '[]' as a shortcut for a put method call or get method call made on a Map type value.
Errors
- If a value other than a
Maptype value is accessed.
Grammar
map_access: '[' expression ']'
Examples
Map access with the
Maptype.Map map = new HashMap(); map['value2'] = 2; map['value5'] = 5; int x = map['value2'] + map['value5']; String y = 'value5'; int z = x[z];- declare
Map map; allocateHashMapinstance →HashMap reference; implicit castHashMap referencetoMap reference→Map reference; storeMap referencetomap - load from
map→Map reference; callputonMap referencewith arguments(String 'value2',int 2) - load from
map→Map reference; callputonMap referencewith arguments(String 'value5',int 5) - declare
int x; load frommap→Map reference; callgetonMap referencewith arguments(String 'value2') →def; implicit castdeftoint 2→int 2; load frommap→Map reference; callgetonMap referencewith arguments(String 'value5') →def; implicit castdeftoint 5→int 5; addint 2andint 5→int 7; storeint 7tox - declare
String y; storeString 'value5'toy - declare
int z; load frommap→Map reference; load fromy→String 'value5'; callgetonMap referencewith arguments(String 'value5') →def; implicit castdeftoint 5→int 5; storeint 5toz
- declare
Map access with the
deftype.def d = new HashMap(); d['value2'] = 2; d['value5'] = 5; int x = d['value2'] + d['value5']; String y = 'value5'; def z = d[y];- declare
def d; allocateHashMapinstance →HashMap reference; implicit castHashMap referencetodef→def; storedeftod - load from
d→def; implicit castdeftoHashMap reference→HashMap reference; callputonHashMap referencewith arguments(String 'value2',int 2) - load from
d→def; implicit castdeftoHashMap reference→HashMap reference; callputonHashMap referencewith arguments(String 'value5',int 5) - declare
int x; load fromd→def; implicit castdeftoHashMap reference→HashMap reference; callgetonHashMap referencewith arguments(String 'value2') →def; implicit castdeftoint 2→int 2; load fromd→def; callgetonHashMap referencewith arguments(String 'value5') →def; implicit castdeftoint 5→int 5; addint 2andint 5→int 7; storeint 7tox - declare
String y; storeString 'value5'toy - declare
def z; load fromd→def; load fromy→String 'value5'; callgetonHashMap referencewith arguments(String 'value5') →def; storedeftoz
- declare
Use the new instance operator 'new ()' to allocate a reference type instance to the heap and call a specified constructor. Implicit boxing/unboxing is evaluated as necessary per argument during the constructor call.
An overloaded constructor is one that shares the same name with two or more constructors. A constructor is overloaded based on arity where the same reference type name is re-used for multiple constructors as long as the number of parameters differs.
Errors
- If the reference type name doesn’t exist for instance allocation.
- If the number of arguments passed in is different from the number of specified parameters.
- If the arguments cannot be implicitly cast or implicitly boxed/unboxed to the correct type values for the parameters.
Grammar
new_instance: 'new' TYPE '(' (expression (',' expression)*)? ')';
Examples
- Allocation of new instances with different types.
Map m = new HashMap();
def d = new ArrayList();
def e = new HashMap(m);
- declare
Map m; allocateHashMapinstance →HashMap reference; implicit castHashMap referencetoMap reference→Map reference; storeMap referencetom; - declare
def d; allocateArrayListinstance →ArrayList reference; implicit castArrayList referencetodef→def; storedeftod; - declare
def e; load fromm→Map reference; allocateHashMapinstance with arguments (Map reference) →HashMap reference; implicit castHashMap referencetodef→def; storedeftoe;
Use the string concatenation operator '+' to concatenate two values together where at least one of the values is a String type.
Grammar
concatenate: expression '+' expression;
Examples
String concatenation with different primitive types.
String x = "con"; String y = x + "cat"; String z = 4 + 5 + x;- declare
String x; storeString "con"tox; - declare
String y; load fromx→String "con"; concatString "con"andString "cat"→String "concat"; storeString "concat"toy - declare
String z; addint 4andint 5→int 9; concatint 9andString "9concat"; storeString "9concat"toz; (note the addition is done prior to the concatenation due to precedence and associativity of the specific operations)
- declare
String concatenation with the
deftype.def d = 2; d = "con" + d + "cat";- declare
def; implicit castint 2todef→def; storedefind; - concat
String "con"andint 2→String "con2"; concatString "con2"andString "cat"→String "con2cat"implicit castString "con2cat"todef→def; storedeftod; (note the switch in type ofdfrominttoString)
- declare
An elvis consists of two expressions. The first expression is evaluated with to check for a null value. If the first expression evaluates to null then the second expression is evaluated and its value used. If the first expression evaluates to non-null then the resultant value of the first expression is used. Use the elvis operator '?:' as a shortcut for the conditional operator.
Errors
- If the first expression or second expression cannot produce a
nullvalue.
Grammar
elvis: expression '?:' expression;
Examples
Elvis with different reference types.
List x = new ArrayList(); List y = x ?: new ArrayList(); y = null; List z = y ?: new ArrayList();- declare
List x; allocateArrayListinstance →ArrayList reference; implicit castArrayList referencetoList reference→List reference; storeList referencetox; - declare
List y; loadx→List reference;List referenceequalsnull→false; evaluate 1st expression:List reference→List reference; storeList referencetoy - store
nulltoy; - declare
List z; loady→List reference;List referenceequalsnull→true; evaluate 2nd expression: allocateArrayListinstance →ArrayList reference; implicit castArrayList referencetoList reference→List reference; storeList referencetoz;
- declare