Win a copy of Terraform in Action this week in the Cloud 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Boolean Wrappers?

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,

This code below is from Dan Chisholm,



How come it prints true for b1 == b2?? Confused.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe the catch here is the valueOf().
When two wrappers are compared with ==, the references only are compared.
b1 == b2 will be false when b1 = new Boolean(true) & b2 = new Boolean(true).
But since valueOf() is used here, it gives the same object to both b1 & b2.

Hope this helps.
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All the Boolean objects (unless created with new operator)are same if there boolean value are same.So here all the references b1,b2,b3,b4 will retrun true when checked with == amongst themselfs.
The same is true for Character(ascii from 0 to 127),Interger(-128 to 127)),Short,Byte.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do you mean
Integer i1 = Integer.valueOf("1"), i2 = Integer.valueOf("1"),
i3 = Integer.valueOf("150"), i4 = Integer.valueOf("150")
System.out.println(i1==i2);
System.out.println(i3==i4);

will result in
true
false

i am getting
false
false
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thing is that Integer.valueOf(String) always returns a new Integer while Boolean.valueOf(String) uses the constants Boolean.TRUE and Boolean.FALSE every time you invoke it.

Therefore, Boolean.valueOf(String) always returns the same object. That is not the case of Integer.valueOf(String).

Something similiar happens if you use Integer.valueOf(int), since the first 128 Integers are cached.



In this case a and b are the same object, while c and d are different objects.
[ December 21, 2006: Message edited by: Edwin Dalorzo ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic