As demonstrated below, I have two functions who's arg types differ (String,int). I now have a third function
public void foo(String bar){}
public void foo(int bar){}
public boolean cond(){ return Math.random() > 0.5 ? true : false; }
public void runsBad(){
String str = "";
int i = 0;
foo( cond() ? str : i );
}
public void runsGood(){
if( cond() )
foo( str );
else
foo( i );
}
runsGood
runs without problems. However, runsBad
returns the following error:
| Error:
| no suitable method found for foo(cond() ? str : i)
| method foo(int) is not applicable
| (argument mismatch; bad type in conditional expression
| java.lang.String cannot be converted to int)
| method foo(java.lang.String) is not applicable
| (argument mismatch; bad type in conditional expression
| int cannot be converted to java.lang.
Am I doing something wrong, or can Java simply not handle the ternary operator as demonstrated?