Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapper classes: Sense of "valueOf" method?

 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
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();
equals:
double d4 = new Double(args[0]).doubleValue();
so why do we need „valueOf()“ method?

 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
-Barry
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's always more than one way to skin a cat.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic