• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question on Boolean objects

 
Siva Sekhar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 -http://www.geekevaluation.com/Exam/question)
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(b1==b2);
System.out.println(b1==b3);
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
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

HTH!!

Regards,
Sanket
 
Devendra Singh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you...sanket...
your explanation is good
 
Akash Mohapatra
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Devendra,

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.
 
Achalveer Singh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
answer will be
false true true true
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic