I am still confused what is the exact difference between this three methods
For example:
Double d = 19.23456;
System.out.println(d.toString());
System.out.println(Double.toString(d));
System.out.println(String.valueOf(d));
All the above methods print the value, but I would like to know more of a logical meaning and differences between these methods. Which method is suitable for which scenarios?
I have gone through the below link where the accepted answer states toString(parameter) and valueOf(parameter) are the same. Then what about toString
? May be I am missing a significant point or something. If so please turn my attention towards it.
Integer.toString(int i) vs String.valueOf(int i)
I have read the documentation but I couldn't make out the differences after reading it.
According to the official docs:
toString
Returns a string containing a concise, human-readable description of this object.
Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data.
The default implementation is equivalent to the following expression:
getClass().getName() + '@' + Integer.toHexString(hashCode())
toString(parameter)
Returns a string containing a concise, human-readable description of the specified double value
valueOf(parameter)
Converts the specified object to its string representation. If the object is null return the string "null", otherwise use toString() to get the string representation.
Thank you in advance
EDIT:
Well I am satisfied by both the answers provided by Eran and zapl. Each of the answers clears the concepts of this method within a specific perspective. For the sake of accepting an answer I would be accepting one. Thank You for clearing my doubt.