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.
Related Posts
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments
No comments yet.
Leave a comment