Created
May 19, 2012 18:49
-
-
Save bergmanngabor/2731931 to your computer and use it in GitHub Desktop.
Playing with semantic holes in Java's notions of equality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class IntLongWat { | |
public static void main(String[] args) { | |
System.out.println(new Integer(1).equals(1)); // true | |
System.out.println(new Long(1).equals(1)); // false, it only equals 1L - so much for semantic equivalence | |
System.out.println(new Integer(1).equals(new Long(1))); // false... Y U NO EQUAL? | |
// although | |
System.out.println(new Integer(1) == 1); // true | |
System.out.println(new Long(1) == 1); // true... WAT? they are == but not equals() ? | |
// so then, transitively... | |
// System.out.println(new Integer(1) == new Long(1)); // COMPILE ERROR... CLEVER BASTARD | |
Object oInt = new Integer(1); | |
Object oLong = new Long(1); | |
System.out.println(oInt == oLong); // false, of course - so much for transitivity | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment