Skip to main content

Operators

  • Operators are symbols to perform a particular functionality.
  • Each operator is applicable on a specific type and using the wrong operand will result in an error.

Assignment Operator

  • = operator is used to assign a value to a variable. For example: def string color = "red", def int daysInMonth; daysInMonth = 31; etc.

Arithmetic Operators

  • +, -, *, / - For arithmetic operations. Applicable for int or real types.
Tip

If any one of the operands is real type, the result is real type.

Consideration

Remember to always check that the divisor is not equal to zero.

  • % - Modulus operator. Used to get remainder. Applicable for int types.

Comparison Operators

  • <, >, <=, >= - For comparing two values. Applicable for int, real, Date, Time or DateTime. Returns boolean value.
  • lt(), gt(), lte() and gte() can be used as alternatives to <, >, <= and >= respectively

Equality Operators

  • ==, != - To check whether two values are equal. Returns boolean value.
  • You can also use eq() function as an alternative to == and neq as an alternative to !=
  • When any type is involved in equality checks, the behaviour of == and != is different.
    • Firstly, the value of any type will be casted to the type of the other operand.
    • If the cast is not possible, the result is false.
    • If the cast is possible, then the result is true only if both values are equal.
    • For more details, see any type.
Consideration

Notice that single equals operator = is used for assignment and double equals operator == is used for equality checks.

Boolean Operators

  • && - And operator. Applicable for boolean values
  • || - Or operator. Applicable for boolean values
Tip

&& and || are short-circuit operators which means that the right hand side (rhs) is only evaluated if the result cannot be obtained from the left hand side (lhs) value. For &&, if the lhs is false, rhs is not computed and for ||, if the lhs is true, rhs is not computed

  • ! - Not operator - Applicable on boolean value
  • ^ - Exclusive or (XOR) operator - Applicable for boolean values

Augmented Assignement Operators

  • These are combination of assignment operator with arithmetic or boolean operators.
  • +=,-=,*=,/=,%= - For example:
    def int a = 1;
a += 1; // This is equal to a = a + 1;
a * = 7 // This is equal to a = a * 7;
  • &&=,||=,^= - For example:
    def boolean a = true;
a &&= true; // This is equal to a = a && 1;
a ||= false // This is equal to a = a || 7;

Concatenation Operator

  • + can be used to concatenate two values
  • Applicable for
    • string - "This" + " is " + "awesome" is equivalent to This is awesome.
    • List - [1,2,3] + 4 is equivalent to [1,2,3,4]. It can also be used combine two lists [1,2,3] + [4,5,6] is equivalent to [1,2,3,4,5,6]
    • Map - mapOf(1 to "a", 2 to "b") + mapOf(3 to "c", 4 to "d") is equivalent to mapOf(1 to "a", 2 to "b",3 to "c", 4 to "d")
    • Set - [1,2,3].toSet() + 4 is equivalent to [1,2,3,4]. It can also be used combine two sets [1,2,3].toSet() + [1,3,5,7].toSet() is equivalent to [1,2,3,5,7]

Removal Operator

  • - can be used to remove an element from a List, Set or Map.
  • Applicable for
    • List - [1,2,3,4] - [2,3] is equivalent to [1,4]
    • Set - [1,2,3,1].toSet() - [2,3].toSet() is equivalent to [1,4]
    • Map - mapOf(1 to "a", 2 to "b",3 to "c", 4 to "d") - [1,2] is equivalent to mapOf(3 to "c", 4 to "d")

String Repeat Operator

  • * can be used to repeat a string.
  • "cool" * 3 is equivalent to "coolcoolcool"
  • Note that you cannot put the int operand on the left hand side of the assignment operator. For example: def string name = "John"; 3 * name = "Doe"; will throw an error.

Conditional Ternary Operator

  • This is a shorthand for writing simple if statements (See Control flow)
  • It is written as <condition> ? <true_value>: <false_value>
def boolean didIWin = true;
def string emotion = didIWin ? "happy" : "sad";
def int a = 10;
def int b = 20;
def int result = a > b ? a : b; // result will be 20
// Let's say we have functions doSomething1() and doSomething2() which return void
a > b ? doSomething1() : doSomething2(); // This will throw a compilation error
// The correct way to write this is:
if (a > b) {
doSomething1();
} else {
doSomething2();
}

Set Inclusion Operators

  • < - Used to check if lhs set is a subset of rhs set
  • > - Used to check if lhs set is a superset of rhs set
  • <= - Used to check if lhs set is a pure subset of rhs set
  • >= - Used to check if lhs set is a pure superset of rhs set
def Set<int> a = [1,2,3];
def Set<int> b = [1,2,3,4,5];
a < b; // This will return true
a > b; // This will return false
def Set<int> a = [1,2,3];
def Set<int> b = [1,2,3,4,5];
a <= b; // This will return true
a >= b; // This will return false