This week's giveaway is in the Android forum. We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line! See this thread for details.

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); } }

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.

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.