Do you know Java: One more more more time
Operators have arity. Sometimes we can make patterns out of them. Which is funny.
Every operator has an arity in Java. They can be either unary, binary, and ternary operators. Arity decides how many operands are accepted in the operation. This brings us the next riddle.
The Riddle
What is the output of the next operation?
class Main {
public static void main(String[] args) {
int n = 0;
int i = 1;
n = i + + + + + + i;
System.out.println(n);
}
}
A few of the operators have two arities, they can be both unary and binary operators.
Plus (+
) and minus (-
) operators are both unary and binary operators.
Once we start playing them, we find more interesting and funnier operations than pluses. What if we mix two of them?
n = i + - + - + - i;
System.out.println(n);
Obviously, those operators that are not unary and binary at the same time, cannot work this way.
n = 2 * * * * * * i;
System.out.println(n);
boolean b = false;
b = true ! ! ! ! ! ! ! false;
System.out.println(b);
In this post we see how funny if an operator has more than one arity.
Code can be found: https://github.com/torokmark/do-you-know-java