• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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: 9692
42
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
 
When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic