This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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.
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
posted
0
>>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.
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.
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
posted
0
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
posted
0
Thanks Pravin, Krishnan, Sergio, Zhao and Dan for clarifying!
Neha Monga
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.