NOTE: You are looking at documentation for an older release. For the latest information, see the current release documentation.
Operators: General
editOperators: General
editPrecedence
editUse the precedence operator '()'
to guarantee the order of evaluation for an
expression. An expression encapsulated by the precedence operator (enclosed in
parentheses) overrides existing precedence relationships between operators and
is evaluated prior to other expressions in inward-to-outward order.
Grammar
precedence: '(' expression ')';
Examples
-
Precedence with numeric operators.
declare
int x
; addint 5
andint 4
→int 9
; multiplyint 9
andint 6
→int 54
; storeint 54
tox
; (note the add is evaluated before the multiply due to the precedence operator)declare
int y
; load fromx
→int 54
; subtractint 50
fromint 54
→int 4
; divideint 12
byint 4
→int 3
; storeint 3
toy
; (note the subtract is evaluated before the divide due to the precedence operator)
Function Call
editUse the function call operator ()
to call an existing function. A
function call is defined within a script.
Grammar
function_call: ID '(' ( expression (',' expression)* )? ')'';
Examples
Cast
editAn explicit cast converts the value of an original type to the equivalent value
of a target type forcefully as an operation. Use the cast operator '()'
to
specify an explicit cast. Refer to casting for more
information.
Conditional
editA conditional consists of three expressions. The first expression is evaluated
with an expected boolean result type. If the first expression evaluates to true
then the second expression will be evaluated. If the first expression evaluates
to false then the third expression will be evaluated. The second and third
expressions will be promoted if the evaluated values are not the
same type. Use the conditional operator '? :'
as a shortcut to avoid the need
for a full if/else branch in certain expressions.
Errors
- If the first expression does not evaluate to a boolean type value.
- If the values for the second and third expressions cannot be promoted.
Grammar
conditional: expression '?' expression ':' expression;
Promotion
byte |
short |
char |
int |
long |
float |
double |
Reference |
def |
|
byte |
int |
int |
int |
int |
long |
float |
double |
- |
def |
short |
int |
int |
int |
int |
long |
float |
double |
- |
def |
char |
int |
int |
int |
int |
long |
float |
double |
- |
def |
int |
int |
int |
int |
int |
long |
float |
double |
- |
def |
long |
long |
long |
long |
long |
long |
float |
double |
- |
def |
float |
float |
float |
float |
float |
float |
float |
double |
- |
def |
double |
double |
double |
double |
double |
double |
double |
double |
- |
def |
Reference |
- |
- |
- |
- |
- |
- |
- |
Object @ |
def |
def |
def |
def |
def |
def |
def |
def |
def |
def |
def |
@ If the two reference type values are the same then this promotion will not occur.
Examples
-
Evaluation of conditionals.
boolean b = true; int x = b ? 1 : 2; List y = x > 1 ? new ArrayList() : null; def z = x < 2 ? x : 2.0;
declare
boolean b
; storeboolean true
tob
declare
int x
; load fromb
→boolean true
evaluate 1st expression:int 1
→int 1
; storeint 1
tox
declare
List y
; load fromx
→int 1
;int 1
greater thanint 1
→boolean false
; evaluate 2nd expression:null
→null
; storenull
toy
;declare
def z
; load fromx
→int 1
;int 1
less thanint 2
→boolean true
; evaluate 1st expression: load fromx
→int 1
; promoteint 1
anddouble 2.0
: resultdouble
; implicit castint 1
todouble 1.0
→double 1.0
; implicit castdouble 1.0
todef
→def
; storedef
toz
;
Assignment
editUse the assignment operator '='
to store a value in a variable or reference
type member field for use in subsequent operations. Any operation that produces
a value can be assigned to any variable/field as long as the
types are the same or the resultant type can be
implicitly cast to the variable/field type.
See variable assignment for examples using variables.
Errors
- If the type of value is unable to match the type of variable or field.
Grammar
assignment: field '=' expression
Examples
The examples use the following reference type definition:
name: Example non-static member fields: * int x * def y * List z
-
Field assignments of different type values.
declare
Example example
; allocateExample
instance →Example reference
; storeExample reference
toexample
load from
example
→Example reference
; storeint 1
tox
ofExample reference
load from
example
→Example reference
; implicit castdouble 2.0
todef
→def
; storedef
toy
ofExample reference
load from
example
→Example reference
; allocateArrayList
instance →ArrayList reference
; implicit castArrayList reference
toList reference
→List reference
; storeList reference
toz
ofExample reference
-
A field assignment from a field access.
declare
Example example
; allocateExample
instance →Example reference
; storeExample reference
toexample
load from
example
→Example reference
; storeint 1
tox
ofExample reference
load from
example
→Example reference @0
; load fromexample
→Example reference @1
; load fromx
ofExample reference @1
→int 1
; implicit castint 1
todef
→def
; storedef
toy
ofExample reference @0
; (noteExample reference @0
andExample reference @1
are the same)
Compound Assignment
editUse the compound assignment operator '$='
as a shortcut for an assignment
where a binary operation would occur between the variable/field as the
left-hand side expression and a separate right-hand side expression.
A compound assignment is equivalent to the expression below where V is the variable/field and T is the type of variable/member.
V = (T)(V op expression);
Operators
The table below shows the available operators for use in a compound assignment. Each operator follows the casting/promotion rules according to their regular definition. For numeric operations there is an extra implicit cast when necessary to return the promoted numeric type value to the original numeric type value of the variable/field and can result in data loss.
Operator |
Compound Symbol |
Multiplication |
*= |
Division |
/= |
Remainder |
%= |
Addition |
+= |
Subtraction |
-= |
Left Shift |
<⇐ |
Right Shift |
>>= |
Unsigned Right Shift |
>>>= |
Bitwise And |
&= |
Boolean And |
&= |
Bitwise Xor |
^= |
Boolean Xor |
^= |
Bitwise Or |
|= |
Boolean Or |
|= |
String Concatenation |
+= |
Errors
- If the type of value is unable to match the type of variable or field.
Grammar
compound_assignment: ( ID | field ) '$=' expression;
Note the use of the $=
represents the use of any of the possible binary
operators.
Examples
-
Compound assignment for each numeric operator.
int i = 10; i *= 2; i /= 5; i %= 3; i += 5; i -= 5; i <<= 2; i >>= 1; i >>>= 1; i &= 15; i ^= 12; i |= 2;
declare
int i
; storeint 10
toi
load from
i
→int 10
; multiplyint 10
andint 2
→int 20
; storeint 20
toi
; (note this is equivalent toi = i*2
)load from
i
→int 20
; divideint 20
byint 5
→int 4
; storeint 4
toi
; (note this is equivalent toi = i/5
)load from
i
→int 4
; remainderint 4
byint 3
→int 1
; storeint 1
toi
; (note this is equivalent toi = i%3
)load from
i
→int 1
; addint 1
andint 5
→int 6
; storeint 6
toi
; (note this is equivalent toi = i+5
)load from
i
→int 6
; subtractint 5
fromint 6
→int 1
; storeint 1
toi
; (note this is equivalent toi = i-5
)load from
i
→int 1
; left shiftint 1
byint 2
→int 4
; storeint 4
toi
; (note this is equivalent toi = i<<2
)load from
i
→int 4
; right shiftint 4
byint 1
→int 2
; storeint 2
toi
; (note this is equivalent toi = i>>1
)load from
i
→int 2
; unsigned right shiftint 2
byint 1
→int 1
; storeint 1
toi
; (note this is equivalent toi = i>>>1
)load from
i
→int 1
; bitwise andint 1
andint 15
→int 1
; storeint 1
toi
; (note this is equivalent toi = i&2
)load from
i
→int 1
; bitwise xorint 1
andint 12
→int 13
; storeint 13
toi
; (note this is equivalent toi = i^2
)load from
i
→int 13
; bitwise orint 13
andint 2
→int 15
; storeint 15
toi
; (note this is equivalent toi = i|2
) -
Compound assignment for each boolean operator.
declare
boolean b
; storeboolean true
inb
;load from
b
→boolean true
; boolean andboolean true
andboolean false
→boolean false
; storeboolean false
tob
; (note this is equivalent tob = b && false
)load from
b
→boolean false
; boolean xorboolean false
andboolean false
→boolean false
; storeboolean false
tob
; (note this is equivalent tob = b ^ false
)load from
b
→boolean true
; boolean orboolean false
andboolean true
→boolean true
; storeboolean true
tob
; (note this is equivalent tob = b || true
) -
A compound assignment with the string concatenation operator.
-
A compound assignment with the
def
type. -
A compound assignment with an extra implicit cast.