File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Boolean.valueOf(boolean) vs Boolean.valueOf(String) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Boolean.valueOf(boolean) vs Boolean.valueOf(String)" Watch "Boolean.valueOf(boolean) vs Boolean.valueOf(String)" New topic

Boolean.valueOf(boolean) vs Boolean.valueOf(String)

Abhishek Asthana
Ranch Hand

Joined: Sep 08, 2004
Posts: 146
From the Boolean javadoc:


What I am not able to understand is that since for both methods return type is same, how come for one a new Boolean instance is being returned and for the other boolean primitive is being returned.

Any pointers will help.

Matthew Brown

Joined: Apr 06, 2010
Posts: 4543

What makes you think a boolean primitive is being returned by either of them, since the Javadocs say that both return a Boolean object?
Abhishek Asthana
Ranch Hand

Joined: Sep 08, 2004
Posts: 146
Detailed from the javadoc:

Returns a Boolean instance representing the specified boolean value. If the specified boolean value is true, this method returns Boolean.TRUE; if it is false, this method returns Boolean.FALSE.

b - a boolean value.
a Boolean instance representing b.

Returns a Boolean with a value represented by the specified String. The Boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".
Example: Boolean.valueOf("True") returns true.
Example: Boolean.valueOf("yes") returns false.

s - a string.
the Boolean value represented by the string.

And since the second method does not return an instance, it is recommended to be used instead of constructor Boolean(String value).

Henry Wong

Joined: Sep 28, 2004
Posts: 20521

Abhishek Asthana wrote:Detailed from the javadoc:

Agreed with Matthew. And I am not sure how this detail from javadoc is to be used to confirm the original question. The JavaDoc clearly states that both methods return a Boolean instance, and not a primative.

[EDIT] Oh I see now. In the example of the Javadoc (and not in the latest java 6 doc), the author mistakenly used lower case. Regardless, if you look at the signature of the method, two lines up, it is obvious that it is a documentation error.... And BTW, it looks like the java 6 javadoc remove those statements entirely.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: Boolean.valueOf(boolean) vs Boolean.valueOf(String)
It's not a secret anymore!