Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Boxing query

 
Ranch Hand
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
15.21.1 Numerical Equality Operators == and !=

5.6.2 Binary Numeric Promotion
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic