public class Test1 {
public static void main(String[] args) {
int a = 3;
Integer a1 = new Integer(a);
Integer a2 = Integer.valueOf(a); // converting int into Integer
Integer a3 = a; //autoboxing, now compiler will write Integer.valueOf(a) internally
System.out.println(a1 instanceof Integer);
System.out.println(a2 instanceof Integer);
System.out.println(a3 instanceof Integer);
int a4 = a1.intValue(); // converting Integer to int
int a5 = a1; // unboxing, now compiler will write a.intValue() internally
}
}
java 拆箱装箱
最新推荐文章于 2025-06-03 01:22:22 发布
这个Java代码示例展示了自动装箱(auto-boxing)和拆箱(unboxing)的过程,以及Integer对象与基本类型int之间的转换。通过`Integer.valueOf()`方法将int转换为Integer对象,而`intValue()`方法则用于将Integer对象转换回int。此外,当基本类型与对应的包装类之间进行赋值操作时,会自动进行装箱或拆箱操作。
519

被折叠的 条评论
为什么被折叠?



