This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in boxing" Watch "doubt in boxing" New topic

doubt in boxing

Tashi Rautela

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(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

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:

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


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


Tashi Rautela

Joined: Jun 27, 2007
Posts: 22
thanks to all...understood
I agree. Here's the link:
subject: doubt in boxing
Similar Threads
== Operation with Boolean
Boolean doubt
Please explain boxing conversion
Boxing query
Boxing conversions question