File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question on Boolean objects 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 "question on Boolean objects" Watch "question on Boolean objects" New topic

question on Boolean objects

Siva Sekhar
Ranch Hand

Joined: Jun 11, 2008
Posts: 37
Can any one explain the following...
THe answer is...(d)false true true false...
Here b1 &b2 are two different references...
and b3 and b4 refereing to same...thats why b3==b4 returns true...
am I wrong...?

Question 19 This is from -
public class etattva6 {
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);

What is the output for the above program?

Your Answer
false false true true

true true true true

false false false false

false true true false
Sanket Meghani

Joined: Jan 15, 2007
Posts: 14
Looks a bit tricky,

When we use == operator for references, it will check if both the references refer to same objects(not the value!!) or not. On the other hand when we use == with standard datatypes, then it will check if their values are same or not.

Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true);
boolean b3 = true;
Boolean b4 = true;

1. b1 == b2 : Here b1 and b2 are refernces and they are refering to two different objects. Hence b1 == b2 is false

2. b1 == b3 : Here b1 is a reference and b2 is a boolean variable. So when we write b1 == b3, the boolean value of b1 gets unboxed (refer Autoboxing/Unboxing) and will be compared with value of b3. So this becomes actual value comparison rather than reference equalization. And as both unboxed value of b1 and value of b3 are true, b1 == b3 is true.

3. b3 == b4 : Same as 2 above. Here value of b4 gets unboxed.

4. b1 == b4 : Same as 1 above.

Hence, D : false, true, true, false is correct answer.

In summary, while operating with boolean in an operation, if one operand is refernce to Boolean and other is a standard boolean datatype, then the value of Boolean refernce gets Unboxed and then the operation is performed between two simple boolean data.


Devendra Singh

Joined: Jun 18, 2008
Posts: 2
i think its all
true true true true
because due to compile time optimization first two reference will point to same object.
and for b1==b3 and b2==b4 it will apply autoboxing.
Siva Sekhar
Ranch Hand

Joined: Jun 11, 2008
Posts: 37
Thank you...sanket...
your explanation is good
Akash Mohapatra

Joined: Aug 20, 2007
Posts: 17

The memory optimization would have happened only if the Boolean references had been declared as

So the point here is to be noted that a new object has to be created at runtime if the new keyword is used.

Hence the answer to b1==b2 is false as they both refer to different objects.

SCJP 6.0, SCWCD 1.5
Achalveer Singh

Joined: Apr 28, 2008
Posts: 8
answer will be
false true true true
GeeCON Prague 2014
subject: question on Boolean objects