Skip to content

Instantly share code, notes, and snippets.

@bergmanngabor
Created May 19, 2012 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bergmanngabor/2731931 to your computer and use it in GitHub Desktop.
Save bergmanngabor/2731931 to your computer and use it in GitHub Desktop.
Playing with semantic holes in Java's notions of equality
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