• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt - 'valueOf' method

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>>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);

....
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 37
IntelliJ IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Neha Monga
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Pravin, Krishnan, Sergio, Zhao and Dan for clarifying!

Neha Monga
reply
    Bookmark Topic Watch Topic
  • New Topic