aspose file tools*
The moose likes Beginning Java and the fly likes Difference between valueOf()Method and toString()Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between valueOf()Method and toString()Method" Watch "Difference between valueOf()Method and toString()Method" New topic
Author

Difference between valueOf()Method and toString()Method

Arun raghvan
Ranch Hand

Joined: Aug 22, 2010
Posts: 75
toString() method is used to convert the value into String

valueOf() method which returns the string representation of the corresponding arguments argument.

both are used for same purpose...i am bit confused over here can some someone explain me
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Object.toString() is used for objects.
String.valueOf(primitive) for primitives.
String.valueOf(object) returns "null" if the object is null else object.toString()

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Arun raghvan
Ranch Hand

Joined: Aug 22, 2010
Posts: 75
Wouter Oet wrote:Object.toString() is used for objects.
String.valueOf(primitive) for primitives.
String.valueOf(object) returns "null" if the object is null else object.toString()


i'm sorry,
not getting .. String class also contains toString() method and valueOf() Method. Both are used to Convert parameter and give it in String Format . So what is the difference
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Arun raghvan wrote:Both are used to Convert parameter and give it in String Format . So what is the difference


No. Only the String.valueOf method is used to get the String represenation of it's parameter object. The toString method (which all classes have) does not take a parameter.

You can use the String.valueOf method to get the String representation of an object without worrying about null references. If you called toString on a null reference you would get a NullPointerException.


Joanne
Arun raghvan
Ranch Hand

Joined: Aug 22, 2010
Posts: 75
Got in Snap !!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between valueOf()Method and toString()Method