This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
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

Question from Dans Test

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question appears in one of Dans Tests
Question 9
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 provided is h. Prints: true,true,true
But there is no Boolean.valueOf(boolean) method. I think the answer should be compile-time error. I tried compiling and I get the same error. I have an exam tomorrow. Can anyone verify the same?
 
Sachin Tendulkar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I just read the answer properly. It says that the valueOf menthod for Boolean class was introduced in ver 1.4. I was compiling my code with 1.3.
I would like to know if there are any other methods that were added to the Wrapper classes in 1.4
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you're mistaken!
I looked up in the java docs and found 2 methods:
public static Boolean valueOf(boolean b);
public static Boolean valueOf(String s);
Have you typed it correctly ? :roll:
Ana
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone tell me how
Boolean b1 = Boolean.valueOf(true);
Boolean b2 = Boolean.valueOf(true);
boolean equal = (b1== b2);
Why is "equal" true? Shouldn't the references b1 and b2 be different
since they are referring to 2 different objects? I would think
that b1.equals(b2) would return true, but not b1==b2....
Thanks
Zak Nixon
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Zak Nixon:
Can someone tell me how
Boolean b1 = Boolean.valueOf(true);
Boolean b2 = Boolean.valueOf(true);
boolean equal = (b1== b2);
Why is "equal" true?


Keep in mind that the wrapper classes are immutable - they can't be changed once they have been created. Because of this, the JVM can make a few optimizations.
One of those optimizations is to have constant references to immutable objects (this is just like the String literal pool, if you're familiar with that). So, when you use the valueOf method of Boolean, rather than creating a new object which is going to be just like an existing one, a reference to the already existing object is returned, instead. Therefore, the == operator returns true, not false.
Note that is only done with immutable objects. Mutable objects, such as StringBuffers do not behave this way.
I hope that helps,
Corey
 
Zak Nixon
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, lets try your theory out:
If all wrapper classes are immutable, then it should work for
all wrapper classes, just as with Boolean.
Lets try the Integer class:

This prints: Equals: false
Can you explain why this happens as well?
Thanks for your help.
Zak Nixon
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Zak,
The boolean type has only two values, true and false. The Boolean wrapper class declares two static final Boolean members as shown below.

The Boolean.valueOf method is declared as follows.

The valueOf method always returns a reference to one of two static final Boolean instances. For that reason, the following equality expression

is equivalent to the following expression.
 
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