File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in boxing

 
Tashi Rautela
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}

the answer is false true true false how??? anyone please explain
 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The first if statement is checking for the references, and since each object refers to a different object on the heap, you'll get false.

The second if statement automatically un-boxes b1 and compares it with b3 which is true, so you'll get true.

The third if statement automatically un-boxes b4 (which was automatically boxed) and compares it with b3 which is true, so you'll get true.

The fourth statement is also checking for references (because, remember, b4 is an automatic boxing), so you'll get false.

Best of luck ...
 
Quintin Stephenson
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tashi

Read the following forum:
http://www.coderanch.com/t/262155/java-programmer-SCJP/certification/equals


It deals with the same set of topics (shallow vs. deep comparisons and autoboxing)

Cheers
Q
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Thanks,
 
Tashi Rautela
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to all...understood
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic