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 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()
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
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.