0

On a slide in this Presentation on Project Valhalla (around 4:00) i stumbled across this Java gotcha:

Integer.valueOf(42) == Integer.valueOf(42);

BUT

Integer.valueOf(420) != Integer.valueOf(420);

Can somebody explain?

Philzen
  • 3,945
  • 30
  • 46
  • this is comparing the object, not value. so you have to use the equals method – Faiz Akram Mar 25 '20 at 03:55
  • 1
    @FaizAkram yeah i know - but still that doesn't explain why one evaluates to true and the other doesn't. https://stackoverflow.com/questions/20877086/why-do-comparisons-with-integer-valueofstring-give-different-results-for-12#answer-20877283 does though. – Philzen Mar 25 '20 at 04:09
  • 1
    the values 0..255 are cached always returning the same value and from 256 onwards a new value is created thats why are different. – chiperortiz Apr 08 '20 at 10:20

0 Answers0