This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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: 3432
    
  12
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 !!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Difference between valueOf()Method and toString()Method
 
Similar Threads
Reading Elements from an ArrayList, 3 at a time
"int cannot be deferenced"
basic thing about toString,equal(),hashCode()
valueOf( ) and toString( )
Difference bet valueOf() and toString()