Can Java’s == operator be asymmetric for primitive values, so that x == y, but y != x, where x and y are both variables of some (possibly different) primitive types?
Edit:
OK, I’ll be honest with the reason why I ask. In the Java Puzzlers book, there is this puzzle #87 which concerns the == operator for primitive types.
It has three parts, one challenges reader to find a case where the == operator is not reflective, second is for finding a case where == is not transitive. I’ve found solutions for both of them, but I have no idea how to solve the third one, which is defined like this:
public class Symmetric {
public static void main(String[] args) throws Exception {
/*
* If you can come up with a set of primitive types and values
* that causes this program to print "true false", then
* you have proven that the == operator is not symmetric.
*/
<typeX> x = <valueX>;
<typeY> y = <valueY>;
System.out.print ((x == y) + " ");
System.out.println(y == x);
}
}
I don’t own the book, so I cannot look it up for a solution, I only got across the source files, which don’t contain the solutions.
==Is symmetric for every type…From the spec: