- Logical Negation !
- and, or, xor
- Conditional Operator ? :
- Default Operator ??
Logical Negation !
bool a = !b;
bool a = b == c; bool a = b != c; bool a = b < c; bool a = b > c; bool a = b <= c; bool a = b >= c;
and, or, xor
bool a = b and c; bool a = b or c; bool a = b xor c;
bool a = b in c;
The in operator performs membership tests of various kinds. The kind of membership test is determined by the type of the right hand side of the expression. The resulting value has type bool.
bool a = b !in c;
The !in operator is the negation of the in operator. Note that !in must be written exactly like that, without any blanks after the exclamation sign.
When the right-hand side is the name of a class, the expression tests class membership. The result is true if the left hand side is an instance of the class to the right or of any of its super classes, and false otherwise.
When the right-hand side is the name of an enumeration type, the expression is true if the left hand side matches the integer value of an entry in the enumeration, and false otherwise.
When the right-hand side is the name of a collection (seq/set/map/index), the expression is true if the left hand side is an element in the collection (key element for map and index).
The behavior of the in operator when the right hand side is an object is to call the object's contains method, with the left hand side of the expression as only argument.
The in operator can be used for testing if a character is contained in a string.
Conditional Operator ? :
The conditional operator ?: takes three operands: a condition and two expressions of the same type. The value of the first expression after the condition is returned if the condition is true, and the value of the other expression otherwise.
a > 0 ? b : c
This expression returns b if a is greater than zero, and c otherwise.
Default Operator ??
The default operator ?? takes two operands: two expressions of the same type. The value of the first expression is returned if the expression is considered to be true, and the value of the other expression otherwise.
pln(s ?? "default");
If s is non-null it will be printed, otherwise "default" will be printed.
The default operator is syntactic sugar for the conditional operator. The following two statements are equivalent:
a ?? b
a ? a : b
Please sign in to leave a comment.