所有的类的父类就是Object类,但是基本数据类型不是类,所以如果想将基本数据类型以类的方式进行处理,那么就需要对其进行包装。 以int数据为例,进行一个包装处理的定义 1class Int{2 private int data; // 包装了一个基本数据类型3 public Int(int data){4 this.data = data;5 }6 public int intValue(){7 return this.data;8 }9}10public class Bao{11 public static void main(String args []){12 Object obj = new Int(10); //装箱: 将基本数据类型保存在包装类中13 int x= ((Int)obj).intValue(); //拆箱: 从包装对象中获取基本数据类型14 System.out.println(x*2);15 }1 collapsed line16} 装箱与拆箱demo 1public class Bao{2 public static void main(String args []){3 Integer obj = new Integer(10); // 装箱 1.9版本之后不建议使用4 int num = obj.intValue(); // 拆箱5 System.out.println(num* 2);6 }7} 自动装箱与拆箱——建议使用 1public class Bao{2 public static void main(String args []){3 Integer obj = 10; //自动装箱, 此时不再关心构造方法4 int num = obj; //自动拆箱5 obj++; //包装类对象可以直接参与数学运算6 System.out.println(num);7 }8}