File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Please explain boxing conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Please explain boxing conversion" Watch "Please explain boxing conversion" New topic
Author

Please explain boxing conversion

sadasiva kumar
Ranch Hand

Joined: Oct 30, 2003
Posts: 91
Give me explanation for this program

public class Boxing6 {
public static void main(String[] args) {
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true);
boolean b3 = true;
Boolean b4 = true;
System.out.println(b1==b2);
System.out.println(b1==b3);
System.out.println(b3 == b4);
System.out.println(b1 == b4);
}
}
output is false true true false


SADASIVAKUMAR UTTI, SCJP1.4
A bend in the road is not the end of the road ... unless you fail to make the turn.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by sadasiva kumar:
Give me explanation for this program

public class Boxing6 {
public static void main(String[] args) {
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true);
boolean b3 = true;
Boolean b4 = true;
System.out.println(b1==b2);
System.out.println(b1==b3);
System.out.println(b3 == b4);
System.out.println(b1 == b4);
}
}
output is false true true false


b1 and b2 are object references to different Boolean objects.

b4 is a reference to another Boolean object.

b3 is a primitive boolean.

If you use b3 in an == with a Boolean, the Boolean will be unboxed.

If neither operand is a boolean, then == will be reference comparison.
sadasiva kumar
Ranch Hand

Joined: Oct 30, 2003
Posts: 91
But still i didnt get the point where b1== b3 is true
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
b1 refers to a Boolean object containing true.

Since b3 is a primitive boolean, when you compare b1 and b3 with ==, b1 will be unboxed to a boolean true, and the comparison is made between the booleans.
sadasiva kumar
Ranch Hand

Joined: Oct 30, 2003
Posts: 91
Thank you keith for your reply
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Not a advanced question. Moving...


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please explain boxing conversion