Operators(4.0)

The following operators are valid in the APL language.

OperatorDescriptionExample
Arithmetic operators:
+Addition (numeric types) or string concatenation (if left operand is of string type).
string1 = string2 + string3;
NumFld = NumFld + 1;
Arithmetic operators (only numeric types):
-SubtractionNumFld = NumFld - 2;
*MultiplicationNumFld = NumFld * 3;
/DivisionNumFld = NumFld / 4;
%ModulusNumFld = NumFld % 10;
Unary operators (only primitive integer types, cannot be a UDR field):
++Increment
NumFld++; //Increment after evaluation
++NumFld; //Increment before evaluation 
--Decrement
 NumFld--; //Decrement after evaluation
--NumFld; //Decrement before evaluation;
Bit operators (only integer types):
&Bitwise ANDNumFld = NumFld & 1;
|Bitwise ORNumFld = NumFld | 2;
<<Shift bits leftNumFld = NumFld << 1;
>>Shift bits rightNumFld = NumFld >> 1;
Boolean operators:
==Equal toif (Fld1 == 1)
!=Not Equal toif (Fld1 != 4)
&&Logical ANDif (Fld1 == 1 && Fld2 != 4)
||Logical ORif (Fld1 == 1 || Fld2 != 4)
<=Less than or equal toif (Fld1 <= 5)
<Less thanif (Fld1 < 5)
>=Greater than or equal toif (Fld1 >= 5)
>Greater thanif (Fld1 > 5)
!Notif (! BoolFld)

Type conversions for the arithmetic operators follow the Java standard.

The && and || operators have the same precedence. Both operators have right fixity. For clarity and to avoid errors, it is generally recommended to override the precedence rules by using parentheses in expressions.

Example - Operator precedence

consume {
    boolean a = false;
    boolean b = false;
    boolean c = true;
    boolean x;
    // The following statement will be parsed as 
	// false && (false || true)
    // and evaluate to false
    x = a && b || c;
    debug(x);
    // This will evaluate to true
    x = (a && b) || c;
    debug(x);
}

Java and APL may differ

In some circumstances,  APL and Java are handling numeric values differently.  APL may be automatically expanded to avoid overflow, this applies also to constant expressions.

Example 1: A statement like: long x=1000000000000; is ok in APL, but will not compile in Java.

Example 2: An expression like: long x=1000000*1000000; works as expected in APL. But in Java, it would assign x to the value -727379968, due to overflow.