您对自动装箱何时起作用感到困惑。在这种情况下,您尝试在普通的旧数据类型(int)上使用 Object 方法。
相反,请尝试Integer.valueof():
public class HelloWorld{
public static void main(String []args){
// String s = 23.toString() will not work since a int POD type does not
// have a toString() method.
// Integer.valueOf(23) gets us an Integer Object. Now we can
// call its toString method
String s=Integer.valueof(23).toString();
s=s+"raju";
System.out.println(s);
}
}
如果您将该 int 传递给期望 Integer 作为参数的方法,则自动装箱将起作用。例如:
List<Integer> intList = new ArrayList<>();
// Autoboxing will silently create an Integer here and add it to the list
intList.add(23);
// In this example, you've done the work that autoboxing would do for you
intList.add(Integer.valueof(24));
// At this point, the list has two Integers,
// one equivalent to 23, one equivalent to 24.