aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt  - 'valueOf' method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt  - Watch "Doubt  - New topic
Author

Doubt - 'valueOf' method

Neha Monga
Ranch Hand

Joined: Mar 13, 2007
Posts: 34
Hello Ranchers!

class C {

public static void main(String args[]) {
Boolean b1 = Boolean.valueOf(true);//line 1
Boolean b2 = Boolean.valueOf(true);//line 2
Boolean b3 = Boolean.valueOf("TrUe");
Boolean b4 = Boolean.valueOf("tRuE");
System.out.println((b1==b2)+",");//line 3
System.out.println((b1.booleanValue()==b2.booleanValue())+",");
System.out.println((b3.equals(b4));
}
}

The output is - true,true,true

Per my understanding, valueOf method takes a String argument so the above code should not compile as String is not passed as argument to valueOf method at line 1 and line 2.

Even if String is passed as argument to methods at line 1 and line 2, line 3 should yield 'false' as 'valueOf(String s)' method returns an object.Hence, b1 and b2 are two different objects. Therefore, b1==b2 should give false.

Pls explain.

Thanks
Neha Monga
Pravin Jain
Ranch Hand

Joined: May 20, 2007
Posts: 60
Hello Ranchers!

class C {

public static void main(String args[]) {
Boolean b1 = Boolean.valueOf(true);//line 1
Boolean b2 = Boolean.valueOf(true);//line 2
Boolean b3 = Boolean.valueOf("TrUe");
Boolean b4 = Boolean.valueOf("tRuE");
System.out.println((b1==b2)+",");//line 3
System.out.println((b1.booleanValue()==b2.booleanValue())+",");
System.out.println((b3.equals(b4));
}
}

The output is - true,true,true

Per my understanding, valueOf method takes a String argument so the above code should not compile as String is not passed as argument to valueOf method at line 1 and line 2.

Even if String is passed as argument to methods at line 1 and line 2, line 3 should yield 'false' as 'valueOf(String s)' method returns an object.Hence, b1 and b2 are two different objects. Therefore, b1==b2 should give false.

Pls explain.

Thanks
Neha Monga

The valueOf method has been overloaded since Java 1.4 to take boolean
as a parameter.
Using a valueOf method is different using a constructor.
inv case of constructor a new instance is created whereas
the valueOf method returns instances from the cache whereever
possible since all wrapper classes are immutable.


The Zen of Java Programming.
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
>>the valueOf method returns instances from the cache whereever...
This is true for other primitives also such as char, short,byte, int and long, aslong as the value is between -128 to +127.


Integer i1 = Integer.valueOf(30);
Integer i2 = Integer.valueOf(30);

Long l1 = Long.valueOf(10l);
Long l2 = Long.valueOf(10l);

Character c1 = Character.valueOf((char)100);
Character c2 = Character.valueOf((char)100);

....
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Originally posted by M Krishnan:
>>the valueOf method returns instances from the cache whereever...
This is true for other primitives also such as char, short,byte, int and long, aslong as the value is between -128 to +127.


Not for long


SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Zhao zhenhua
Ranch Hand

Joined: Jul 17, 2006
Posts: 37


class C {

public static void main(String args[]) {
Boolean b1 = Boolean.valueOf(true);//line 1
Boolean b2 = Boolean.valueOf(true);//line 2
Boolean b3 = Boolean.valueOf("TrUe");
Boolean b4 = Boolean.valueOf("tRuE");
System.out.println((b1==b2)+",");//line 3
System.out.println((b1.booleanValue()==b2.booleanValue())+",");
System.out.println((b3.equals(b4));
}
}

The output is - true,true,true


Follow is the source code of value(boolean b)
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
so it can take a boolean value as a argument.

And follow is the source code of value(String s)
public static Boolean valueOf(String s) {
return toBoolean(s) ? TRUE : FALSE;
}
And follow is the source of toBeelean(s)
private static boolean toBoolean(String name) {
return ((name != null) && name.equalsIgnoreCase("true"));
}
So we know that that show when we compare the value "equalsIgnoreCase".
The answer is very clear!


Head first Servlet and JSP
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
The references b1, b2, b3 and b4 all point to exactly the same instance of a Boolean object that wraps the boolean value "true".

The Boolean class contains two static fields, TRUE and FALSE, that are defined as follows.

As shown in previous posts in this thread, the valueOf method returns a reference that is contained in one of those two static fields. In this mock exam question, the reference variables, b1, b2, b3 and b4 were all set by a reference returned from the valueOf method; so all four reference the same instance of a Boolean object that wraps the value "TRUE".

The first of the following three comparisons varifies that the reference values are exactly the same.


The second and third comparisons verifies that the wrapped primitive values are the same.

This question also demonstrates that the valueOf method that accepts an argument of type String ignores the case of the string.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Neha Monga
Ranch Hand

Joined: Mar 13, 2007
Posts: 34
Thanks Pravin, Krishnan, Sergio, Zhao and Dan for clarifying!

Neha Monga
 
Consider Paul's rocket mass heater.
 
subject: Doubt - 'valueOf' method
 
Similar Threads
equals()
valueOf()..
valueOf()
Boolean class
boolean question