29

Output:
true
false

Why?
This question was asked by my friend, was unanswerable.

Comments
  • 8
    Integer is an object wrapper around int. == compares the object and not the value in the integer objects case. I think the autoboxing for integer is only accurate within a relatively small range. Like -100 through 100. I don't do java regularly so I might be off on that. Calling intValue on both should get you the expected result.
  • 6
    You gotta to unbox the value dude. The == in Java checks if the two values are the same reference, not their value. So use Integer.valueOf(int) == Integer.valueOf(int). Or simply, int.equals(int). And String.equals(String) for Strings.

    Remember, Java is super verbose
  • 4
    @grimtar it stops working after 127
  • 12
    values from - 128 to 127 are buffered internally for Integer. If you assign the same value twice, they reference the same buffered value, therefore comparing the reference is true. there are some freaky things with reflection where you can make the outcome of e. g. 4+3=5 etc. Same for other primitive wrappers and strings
  • 0
  • 1
  • 1
    @filthyranter Maybe in Java9, it's a proposed feature.
Add Comment