aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wrapper Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wrapper Class " Watch "Wrapper Class " New topic
Author

Wrapper Class

TH Eee
Greenhorn

Joined: Dec 22, 2006
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
Marshal

Joined: Mar 22, 2005
Posts: 41524
    
  53
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.


Ping & DNS - my free Android networking tools app
TH Eee
Greenhorn

Joined: Dec 22, 2006
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

Joined: Dec 22, 2006
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

Joined: Dec 22, 2006
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

Joined: May 30, 2004
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

Joined: Aug 03, 2002
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
raja kanak
Ranch Hand

Joined: Oct 18, 2006
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?


live
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
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

Joined: Dec 11, 2006
Posts: 81
And how about IntegerCache? Because it's not stay in Integer class API.
Andy Morris
Ranch Hand

Joined: May 30, 2004
Posts: 78
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: 41524
    
  53
IntegerCache is an inner class of Integer, so no need to go looking for it - just find Integer.java, and you've got it.
 
jQuery in Action, 2nd edition
 
subject: Wrapper Class