Integer.valueOf(5) == Integer.valueOf(5)
true
Integer.valueOf(200) == Integer.valueOf(200)
false Integer.valueOf(5) == Integer.valueOf(5)
true
Integer.valueOf(200) == Integer.valueOf(200)
falseYou don't compare non-primitives (which boxed integers are) with == in Java, you use the equals method.
And in fact when I googled to find the exact range where it switches from cached integers to truly creating new objects, the top link was at best highly misleading. https://www.tutorialspoint.com/check-two-numbers-for-equalit...
Obviously Java doesn't have the luxury of that decision at this point. But a lot of goofy parts of languages are string of fairly rational choices with unintended consequences.
If you want a instance, I recommend new :)
Integer val1 = 5;
Integer val2 = 5;
Integer val3 = 200;
Integer val4 = 200;
will have you ending up with val1 and val2 being equal with ==, but val3 and val4 won't be.The general reply is 'you're holding it wrong', but that doesn't make it any less absurd. That's what the PHPers say about "123" < "456A" < "78" < "123".
Unless you call `new` there's no guarantee that you have a different object.