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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…