This week's book giveaway is in the Design forum.We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!

# Wrapper Class

TH Eee
Greenhorn
Posts: 6
I have study the Wrapper Class (Chapter 3) from the K&B Java 5 Book. I just modified the source code given from this book (page 234).

class Test {
public static void main(String[] args) {

Integer a = 130; //1
Integer b = 130;//2
Integer c = 100;
Integer d = 100;

System.out.println("(a==b)" + (a==b)); //3
System.out.println("(c==d)" + (c==d)); //4

}
}

Output:
false
true

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
Rancher
Posts: 42967
73
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.

TH Eee
Greenhorn
Posts: 6
class Test{
public static void main(String[] args) {

Short a = -129;
Short b= -129;
//Byte c = -129;
//test2Byte d = -129;

//System.out.println("(a==b)" + (a==b)); //
//System.out.println("(c==d)" + (c==d)); //
}
}

TH Eee
Greenhorn
Posts: 6
class Test{
public static void main(String[] args) {
Short a = -129;
Short b= -129;
Byte c = -129;
Byte d = -129;

System.out.println("(a==b)" + (a==b));
System.out.println("(c==d)" + (c==d));
}
}

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
Posts: 6
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
Posts: 78
-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!

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html

Barry Gaunt
Ranch Hand
Posts: 7729
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.

raja kanak
Ranch Hand
Posts: 135
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?

Barry Gaunt
Ranch Hand
Posts: 7729
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
Posts: 81
And how about IntegerCache? Because it's not stay in Integer class API.

Andy Morris
Ranch Hand
Posts: 78
What is the IntegerCache's package name?

I'd suggest looking the corresponding directory up in the source.

Ulf Dittmer
Rancher
Posts: 42967
73
IntegerCache is an inner class of Integer, so no need to go looking for it - just find Integer.java, and you've got it.