Operators

Operators are used to perform operations on variables and values

The following table provides information about operators

TypeNameDescription
+AdditionAdds together two values
-SubtractionSubtracts one value from another
*MultiplicationMultiplies two values
/DivisionDivides one value by another
%ModulusReturns the division remainder
&&, andLogical andReturns true if both statements are true
||, orLogical orReturns true if one of the statements is true
!, notLogical notReverse the result, returns false if the result is true
==EqualCompare values for equality
!=Not equalReturns true if values are not equal
>=Greater than or equalReturns true if left value is greater than or equal to right value
<=Less than or equalReturns true if left value is less than or equal to right value
===Strict equalCompare pointer/reference equality (same memory address)
!==Strict not equalCompare raw C++ pointer inequality (different memory address)
+=Add and assignAdd right value to variable and assign the result
-=Subtract and assignSubtract right value from variable and assign the result
*=Multiply and assignMultiply variable by right value and assign the result
/=Divide and assignDivide variable by right value and assign the result

Example

val a = 10 val b = 3 // Arithmetic println(a + b) // 13 println(a - b) // 7 println(a * b) // 30 println(a / b) // 3 println(a % b) // 1 // Comparison println(a == b) // false println(a != b) // true println(a >= b) // true println(a <= b) // false // Logical val x = true val y = false println(x && y) // false println(x and y) // false println(x || y) // true println(x or y) // true println(!x) // false // Assignment var c = 5 c += 2 // 7 c -= 1 // 6 c *= 3 // 18 c /= 2 // 9 println(c) // Strict / pointer comparison val obj1 = 1 val obj2 = obj1 val obj3 = 1 println(obj1 === obj2) // true (same pointer) println(obj1 !== obj3) // true (different pointer)