• 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

Wrapper class object creation question

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this correct and if yes how/why? Why would we not need 'new' to allocate memory to d?

Double d = Double.valueOf("3.14");

thanks
radiya
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Radiya Sojitrawala:
Is this correct and if yes how/why? Why would we not need 'new' to allocate memory to d?

Double d = Double.valueOf("3.14");

thanks
radiya



Hi Radiya,
Well, actually what we are doing is using the inbuilt function, valueOf(), of the Wrapper class Double to convert a String representation of a double number(3.14) back into double.

When we say Double.valueOf("3.14"), it means that we have a String argument "3.14" which we wish to convert into a Double Wrapper Object as 3.14.

This function will return an object of type Double which will be assigned to d. Hence d has to be of type Double too. That's why we do not need the new operator.
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In class Double, the valueOf() method is static. If you check the source code, you'll see that the method is just one line:


So there's the constructor call you were wondering about.

-- Phil
 
Radiya Sojitrawala
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys
 
Crusading Chameleon likes the size of this ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic