• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Boolean ==

 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

One of the mock exams states that the following code

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

produces the following output:

false
true
true
false

Why is this? I understand that the primitive boolean true, false are stored as immutable objects.

Thanks in advance.

-Saha
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Saha Kumar:
Hello All,

One of the mock exams states that the following code

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

produces the following output:

false
true
true
false

Why is this? I understand that the primitive boolean true, false are stored as immutable objects.

Thanks in advance.

-Saha



b1, b2, and b4 refer to different Boolean objects. b3 refers to a boolean primitive. When you use == between object references, it tests whether the references point to the same object. If you use == between a Boolean and boolean, then the Boolean will be autounboxed to a boolean.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

One of the mock exams



Please tell us to which mock exam you are referring.
 
Saha Kumar
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much, Keith. Can the same be said for other cases, such as between:

Integer, int
Float, float
etc.

For example:
Integer a = 200;
int b = 200;

a == b yeilds true because a is autounboxed to int?

Thanks in advance.

-Saha
 
Saha Kumar
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The mock exam referred to above can be found at:

mock exam

-Saha
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Saha Kumar:
Thanks so much, Keith. Can the same be said for other cases, such as between:

Integer, int
Float, float
etc.

For example:
Integer a = 200;
int b = 200;

a == b yeilds true because a is autounboxed to int?

Thanks in advance.

-Saha



Yes, if one of the operands is a primitive, and the other can be unboxed to that primitive, it will be unboxed before the comparison.
 
Saha Kumar
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again, Keith!
 
It's never done THAT before. Explain it to me tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic