Код: Выделить всё
public class A {
public static U m(String s){
U out=null;
try {
out = (U) Integer.valueOf(Integer.parseInt(s));
} catch (NumberFormatException e){
System.out.println("ex int ");
}
if(out==null)
try {
out = (U) Double.valueOf(Double.parseDouble(s));
} catch (NumberFormatException e){
System.out.println("ex double");
}
System.out.println(out.getClass().getName());
return out;
}
public static void main(String[] args) {
String s = new String("10.2");
System.out.println(A.m(s)); // Shouldn't the compiler generate a warning at this point?
}
}
< /code>
Аналогичные темы уже обсуждались (например: Cast Cobject для общего типа для возврата).
Я понял, что из -за стирания типа составленный код будет соответствовать: < /p >
Object out = (Object) Double.valueOf(Double.parseDouble(s));
Подробнее здесь: https://stackoverflow.com/questions/794 ... neric-type