Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Boxing query

 
Pawanpreet Singh
Ranch Hand
Posts: 264
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why the output is
/*

false
true
true
false

*/

for


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


Could anybody explain....
 
Leonardo Luiz
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think it is because:

(b1==b2) and (b1==b4) compares two reference variable pointing to two diferent Boolean instances. (true is boxed into b4);

(b1==b3) compares two primitives (b1 is unboxed).

(b3==b4) compares two primitives.

 
wise owen
Ranch Hand
Posts: 2023
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic