Two Laptop Bag
The moose likes Beginning Java and the fly likes Wrapper classes: Sense of Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Wrapper classes: Sense of "valueOf" method?" Watch "Wrapper classes: Sense of "valueOf" method?" New topic

Wrapper classes: Sense of "valueOf" method?

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
in the following code I present two ways of converting Strings to double primitives
Either by pareDouble or valueOf() in combination with wrapper class Double.
Why is it possible to omit „valueOf()“ method:
double d2 = Double.valueOf(args[0]).doubleValue();
double d4 = new Double(args[0]).doubleValue();
so why do we need „valueOf()“ method?

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The Double.valueOf() (and Double.copyValueOf()) methods are class (static) methods. So doing double trouble = Double.valueOf("2.71828"); is kinda convenient for
going from a String (or whatever valueOf() likes) to a primitive double variable.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
There's always more than one way to skin a cat.

Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
I agree. Here's the link:
subject: Wrapper classes: Sense of "valueOf" method?
jQuery in Action, 3rd edition