• 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

valueOf()..

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I got it from http://www.danchisholm.net/july21/topic/section8/wrapper2ans.html


class Boo
{
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));
System.out.println(b3==b4);
}

}

output is true,true,true.
I thought the answer would be false,true,false. Since the valueOf() returns newly createdobject of the type that invoked the method.

please explain me this comes true...

thanks in advance
Preparing Scjp 1.5
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Preetha Arun:
valueOf() returns newly createdobject of the type that invoked the method.



No value of returns objects from the pool. when you use new Type() syntax, then you get a new object. In autoboxing you get a value from the pool(it it's in the range) because autoboxing uses type.valueOf internally...
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add my 2 cents.

"valueOf" methods are static factory methods. They provide an alternative mechanism for instatiating objects (they are used in a number of places including the wrapper classes). One of their benefits is that they allow the flexibility to determine whether a new instance is created or whether a reference to a previously created instance (from a previosly allocated pool) is returned (this is being an implementation of the Flyweight pattern). In this example the boolean Wrapper class (Boolean) can return the same reference when invoked with Boolean.value("true") without the need to actually create an new object each time. Depending on the exact nature of the program this can potentially add up to a very singificant saving in memory. It is certainly a value tool to understand and apply prudently when your putting Java to work (for more info see Effective Java).

(P.S. I found that understanding the context helped me to remember what valueOf methods took as parameters and what types they returned)

thanks,

Graeme
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to you both. I got it

Preparing Scjp1.5
 
and POOF! You're gone! But look, this tiny ad is still here:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic