I predict that line 3 and 4 will display true. If I replace 130 in line 1 and 2 with 120 , its output will change to true. May I know how I get this output ?
Thank You.
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35252
7
posted
0
That's because for all integers from -128 to 127, there will only ever be a single Integer object representing each of those in the JVM if you create them in this way.
For integers outside of that range (e.g. 130), new Integer objects are created, and there can be more than one, so if you create two, they won't be equal.
If you want to understand how this works in detail, have a look at the source code of the Integer class, and search for "IntegerCache" and the "valueOf(int i)" method.
I modify my program again. I wonder that -129 for Short wrapper class, the output will be false. (as what I had expected) If I test -129 for Byte wrapper class, this program will compile error. The error description is Incompatible Types.
May I know how this could be happen and what does incompatible types mean here? Thank You.
TH Eee
Greenhorn
Joined: Dec 22, 2006
Posts: 6
posted
0
I not understand that if I test Short and Byte wrapper class with -129, why difference output will be given since their range is -128 to 127. For Short wrapper class, the program will display false while for the Byte wrapper class, the program will compile error.
Andy Morris
Ranch Hand
Joined: May 30, 2004
Posts: 78
posted
0
-129 does not fit into a byte without casting.
Bytes have a range of -128 to 127 so a value of -129 is incompatible.
Check the fundamental java primitive data types, you should know them!
And when you have cast -129 to a byte its value is 127. So the == comparison between the autoboxed wrapper objects is going to give a result of true for this case.
Originally posted by Ulf Dittmer: That's because for all integers from -128 to 127, there will only ever be a single Integer object representing each of those in the JVM if you create them in this way.
For integers outside of that range (e.g. 130), new Integer objects are created, and there can be more than one, so if you create two, they won't be equal.
If you want to understand how this works in detail, have a look at the source code of the Integer class, and search for "IntegerCache" and the "valueOf(int i)" method.
Where & How can I find source code of Integer class?
live
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
posted
0
Originally posted by raja kanak:
Where & How can I find source code of Integer class?
src.zip in the Java jdk1.5.0_x distribution.
Tanakorn Numrubporn
Ranch Hand
Joined: Dec 11, 2006
Posts: 81
posted
0
And how about IntegerCache? Because it's not stay in Integer class API.
Andy Morris
Ranch Hand
Joined: May 30, 2004
Posts: 78
posted
0
What is the IntegerCache's package name?
I'd suggest looking the corresponding directory up in the source.
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35252
7
posted
0
IntegerCache is an inner class of Integer, so no need to go looking for it - just find Integer.java, and you've got it.