tags:

views:

212

answers:

3

Hello,

I got the code of a former employee. There are many calls to methods like:

foo(val,...);

where

void foo(String s,...) {
  ...
}

and val is an int.

Of course, I get an error.

As a workaround I pass ""+val to foo. I wonder if there is a better way.

A: 

Unfortunately, no. Without modifying the method definition, that's the quickest and easiest way to do it.

mipadi
+3  A: 

String.valueOf(val) is much faster.

ng
+11  A: 

depending on the different types that are supposed to be passed as parameter, you could either accept an object and call .toString()

void foo(Object o){
   String s=o.toString();
   ...
}

or overload foo for specific types

void foo(String s) {
  ...
}

void foo(int i){
    foo(Integer.toString(i);
}
lcvinny
Personally, I'd use the Object parameter approach.
Brian Clapper