Posted by & filed under Java.

In String type we have several method valueOf

static String   valueOf(boolean b) 
static String   valueOf(char c) 
static String   valueOf(char[] data) 
static String   valueOf(char[] data, int offset, int count) 
static String   valueOf(double d) 
static String   valueOf(float f) 
static String   valueOf(int i) 
static String   valueOf(long l) 
static String   valueOf(Object obj) 

As we can see those method are capable to resolve all kind of numbers

every implementation of specific method like you have presented: So for integers we have

Integer.toString(int i)

for double

Double.toString(dobule d)

and so on

In my opinion this is not some historical thing, but is more useful for developer to use the method valueOf from String class than from proper type, because is less changes to make when we want to change the type that we operate on.

Sample 1:

public String doStaff(int num) {

 //Do something with num 

  return String.valueOf(num);

 }

Sample2:

public String doStaff(int num) {

 //Do somenthing with num

 return Integer.toString(num);

 }

As we see in sample 2 we have to do two changes, in contrary to sample one.

My conclusion is that using the valueOf method from String class is more flexible and that why is available there.

http://stackoverflow.com/questions/3335737/integer-tostringint-i-vs-string-valueofint-i

Comments are closed.