RSS Feed for This PostCurrent Article

Java: toString and valueOf

In Java there are many ways to convert data to different data types. Here are some common tips and observations.

  • Always prefer Object.toString rather than String.valueOf. Internally String.valueOf is calling Object.toString.  E.g. String.valueOf(int) calls Integer.toString and String.valueOf(double) calls Double.toString
  • Use primitive data types rather than boxed primitives
  • Use Object.valueOf instead of new Object. E.g. use Double.valueOf instead of new Double. Well this is just my preference, even though Object.valueOf calls new Object internally
  • Object.toString called String.valueOf internally. E.g. doubleValue.toString calls String.valueOf internally.


Trackback URL


Sorry, comments for this entry are closed at this time.