I've read everywhere that when you define an Integer between -128 to 127 in Java, instead of creating a new object it returns an object already created.
I don't see any point of doing this other than letting newbie programmers compare Integer objects with ==
to see if they are the same number, but I think this is bad because sure they think that they can compare any Integer with ==
, and also is teaching a bad practice in any programming language: comparing the content of two 'different' objects with ==
.
Is there any other reason on why this is done? Or is it just a bad decision when designing the language (In my point of view) like optional semicolon in JavaScript?
EDIT: I see here that they explain the behaviour: Why does the behavior of the Integer constant pool change at 127?
I'm asking why they designed it to have this behaviour, and not why is this behaviour happening.