aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Boxing/Unboxing and equality 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 "Boxing/Unboxing and equality" Watch "Boxing/Unboxing and equality" New topic
Author

Boxing/Unboxing and equality

Joan Pujol
Ranch Hand

Joined: Oct 12, 2005
Posts: 54
Boolean b=new Boolean(true);
boolean b2=true;
System.out.println(b==b2);

Someone can say why this program prints true?
I don't understand because I think that b2 is boxed to Boolean. And then, two diferent instances of Boolean aren't equal, are it?

A lot of thanks in advance,
Akshay Kiran
Ranch Hand

Joined: Aug 18, 2005
Posts: 220
all boolean values are cached.
for more regarding comparisons regarding autoboxed values,
http://java.boot.by/scjp-tiger/ch03.html#c3s1
the description is somewhere in the middle.


"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5207
    
  12

this is something for Tiger, i guess because my compiler gives an error on the println-statement


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You will find that the other wrapper classes exhibit this caching behaviour for some subrange of their values. However, there are special methods which can be used to ensure that you do get a unique object so check the API.

Originally posted by Roel De Nijs:
this is something for Tiger, i guess because my compiler gives an error on the println-statement


Yes - the title Boxing/Unboxing indicates that - it is a Java 5.0 feature.
By the way the Tiger is now dead - it's officially released as Java 5.0.
[ October 20, 2005: Message edited by: Barry Gaunt ]

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

Joined: Oct 12, 2005
Posts: 54
I still don't understand.

Because the documentation says that new Boolean(boolean) creates a new instance.

Then b is a new instance (Boolean b=new Boolean(true) ;)
And b2 when it's boxed is the cached instance...

Why the new instance is equal to the cached one?

A lot of thanks in advance.
[ October 20, 2005: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
There is also the possibility that there is unboxing taking place here. Then the comparison is on two primitives.

Boxing or Unboxing that is the question? Better check this out...


Yes! Section 15.21.2 of the Java Language Specification states that if one of the operands is of type boolean and the other of type Boolean, then it is a boolean equality operation. The Boolean operand is subjected to unboxing. That's why you are getting true for the == operation.
[ October 20, 2005: Message edited by: Barry Gaunt ]
Joan Pujol
Ranch Hand

Joined: Oct 12, 2005
Posts: 54
A lot of thanks.
Akshay Kiran
Ranch Hand

Joined: Aug 18, 2005
Posts: 220
Originally posted by Barry Gaunt:
By the way the Tiger is now dead - it's officially released as Java 5.0.


what's that supposed to mean? the people on java.sun.com still refer to j2se 5.0 as tiger
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Response to off topic post:
Yes, they still refer to it as Tiger, but tiger is an internal code name, see codenames. It is normal for the code name to be dropped after the official release. So yes, Tiger is still used, but J2SE 5.0 is the official name. If you want to be cool you must talk about Mustang now, Tiggers are old hat.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Boxing/Unboxing and equality