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 forintorrealtypes.
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 forinttypes.
Comparison Operators
<,>,<=,>=- For comparing two values. Applicable forint,real,Date,TimeorDateTime. Returnsbooleanvalue.lt(),gt(),lte()andgte()can be used as alternatives to<,>,<=and>=respectively
Equality Operators
==,!=- To check whether two values are equal. Returnsbooleanvalue.- You can also use
eq()function as an alternative to==andneqas an alternative to!= - When
anytype is involved in equality checks, the behaviour of==and!=is different.
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
booleanvalues - || - Or operator. Applicable for
booleanvalues
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
booleanvalue - ^ - Exclusive or (XOR) operator - Applicable for
booleanvalues
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 toThis is awesome.List-[1,2,3] + 4is 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 tomapOf(1 to "a", 2 to "b",3 to "c", 4 to "d")Set-[1,2,3].toSet() + 4is 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 aList,SetorMap.- 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 tomapOf(3 to "c", 4 to "d")
String Repeat Operator
*can be used to repeat a string."cool" * 3is 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