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

Dan's Question : Fundamental Classes

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I don't quite understand the following question:
class C {
public static void main(String[] args) {
Boolean b1 = Boolean.valueOf(true);
Boolean b2 = Boolean.valueOf(true);
Boolean b3 = Boolean.valueOf("TrUe");
Boolean b4 = Boolean.valueOf("tRuE");
System.out.print((b1==b2) + ",");
System.out.print((b1.booleanValue()==b2.booleanValue()) + ",");
System.out.println(b3.equals(b4));
}}
What is the result of attempting to compile and run the program?
a. Prints: false,false,false
b. Prints: false,false,true
c. Prints: false,true,false
d. Prints: false,true,true
e. Prints: true,false,false
f. Prints: true,false,true
g. Prints: true,true,false
h. Prints: true,true,true
i. Compile-time error
j. Run-time error
k. None of the above
The answer is true,true,true. What I am not understand is, for b1==b2, why produces true? I think b1 and b2 refer to 2 Boolean instances, and so the addresses of these two instances should be different, and so b1==b2 should false. But why b1==b2 is true?
Thanks,
David
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David,
The booleanValue method returns the primitive boolean value. It doesn't return an instance of the Boolean wrapper class.
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the JavaDoc for Boolean, you'll see the static fields TRUE and FALSE (both are Boolean objects). When Boolean.valueOf() is invoked, a reference to one of these is returned.
So, given your code example,

would return true, as they all reference the same Boolean object.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ray. I answered the wrong question in my previous response.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi David,
The valueOf() method of Boolean will not create a new instance if there is another instance with the same value! it simply assign the existing one to b2(since thay are both true)! Such design gains better space and time performance.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so does this mean that there are only too boolean objects - true and false?
 
David Jones
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jacky,
Is this the same for the String references? e.g.
String a = new String("ABC");
String b = new String("AB");
b = b + "C";

Is a and b refer to the same object "ABC"?
THanks,
David
 
Ranch Hand
Posts: 168
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shilpi M Agarwal:
so does this mean that there are only too boolean objects - true and false?


The primitive boolean data type has only two possible values: true or false.
But there can be more than two objects of the type Boolean. But if we invoke the method booleanValue() on these objects the return value are always either primitive boolean true or false.
If you need a Boolean object, instead of creating a new one, use the existing ones: Boolean.TRUE and Boolean.FALSE.
Before Java 1.4 :

Java 1.4 :

This method is likely to yield significantly better space and time performance.
[ January 13, 2004: Message edited by: Yosi Hendarsjah ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic