Welcome to Srini's blog

Wednesday, April 14, 2010

String.valueOf() Vs toString() -- Java

1. If variable is primitive datatype(i.e int,float,char...) then it does not have the toString() method available. So If it requires to convert into string we use String.valueOf(variable).

2. If variable is non primitive datatype(i.e Integer,Float, Boolean....) then it has toString() method available. So here toString() is usefull.

3. The String.valueOf(Object) calls through to the toString() method of the given object (if it isn't null). Hence, if String.valueOf(int) is called, the primitive int will be auto-boxed to an Integer, and its toString() method will be called.

So Finally:
String.valueOf(i),
Integer.toString(i) and
Integer.valueOf(i).toString()
all the above stmts gives the same result.

No comments:

Post a Comment