The key to understanding this is to know how an "int" is converted to an "Integer" (a process called "autoboxing"). This is done by using the static Integer.valueOf(int) method. Its javadocs contain a very subtle hint what's going on. You may understand it if you consider these two questions: "Is 2 a frequently used value?" and "Is 2000 a frequently used value?"
Let us know of this helps; if not, we can go into more detail.
Hi, sorry, I cannot say, that Javadoc make it more clear for me:
public static Integer valueOf(int i)
Returns a Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values.
Appreciate additional explanations.
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35249
7
posted
0
public static Integer valueOf(int i)
Returns a Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values.
The hint is: "2" is a frequently requested value, while "2000" is not.
Michael Rootman
Greenhorn
Joined: Apr 15, 2009
Posts: 19
posted
0
Ulf Dittmer wrote:
The hint is: "2" is a frequently requested value, while "2000" is not.
I am really sorry, but this does not make sense for ma at all. What is it "frequently requested value"? How should I know which one is and which is not?
Explanation in exam itself is not clear for me as well:
When the references i and eye in the pool are compared 2==2 results in true and 2000==2000 is false since it exceeds 127.
How 127 is connected to comparing Integer values?
How JVM proceed boxing and unboxing, so same Integer values are equal or not equal depending on how this values big or "frequently requested"?
Internally a cache is maintained to optimize space occupied by each Object. The source code reveals that the Integer class maintains an internal cache of Integers 0 to 127.
This is because you may frequently need the integers in this range.. Moreover this range also includes range of Byte and Short.
Here I am providing the code copied from the Integer class.
Section: 3)How ==, !=, <=, >= works for Boxing/Unboxing ?
Bottom line:
In order to save memory when primitive types are boxed into the wrapper types, the JVM allocates memory and creates a new object. But for some special cases, the JVM reuses the same object, two instances of the following wrapper objects will always be == when their primitive values are the same.
* Boolean
* Byte
* Character from \u0000 to \u007f (7f is 127 in decimal)
* Short and Integer from -128 to 127
Michael Rootman
Greenhorn
Joined: Apr 15, 2009
Posts: 19
posted
0
HI,
Now I can understand the logic in the output.
Thank you to everyone for help!
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.