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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in boxing" Watch "doubt in boxing" New topic
Author

doubt in boxing

Tashi Rautela
Greenhorn

Joined: Jun 27, 2007
Posts: 22
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

Joined: Mar 08, 2007
Posts: 1585
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 ...


Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
Quintin Stephenson
Ranch Hand

Joined: Nov 16, 2006
Posts: 40
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


If at first you don't succeed, try, try again. If you don't try you have failed.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707


Thanks,


cmbhatt
Tashi Rautela
Greenhorn

Joined: Jun 27, 2007
Posts: 22
thanks to all...understood
 
wood burning stoves
 
subject: doubt in boxing