Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

From mock exam about Autoboxing

 
Santosh Ramakrishna
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<pre>

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

</pre>

What is the output for the above program?
1)false true true false
2)false false false false
3)true true true true
4)false false true true

1 is the answer. I have a fairly good understanding of autoboxing and using the object from the pool.

if b1 and b4 are not equal, they are pointing to different objects, which is fine. How b1 == b3 and b3 == b4 both return true?

Thanks for your help.
 
Anton Uwe
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you compare a primitive with a wrapper type (here by using "==", ), the wrapper type will be automatically unboxed to a primitive.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic