封装类跟类型有什么区别?

来源:百度知道 编辑:UC知道 时间:2024/05/30 15:15:10
简单数据类型 封装类
boolean Boolean
byte Byte
short Short
int Integer
long Long
char Character
float Float
double Double
public class Cp {
public void stupid() {
Integer i = new Integer(10);
System.out.println(i);
int i1 = 10;
System.out.println(i);
}

public static void main(String[] args) {
Cp a = new Cp();
a.stupid();

}

}两种输出看起来没什么区别啊

数据类型不一样,boolean ,byte,short等是基本数据类型。
而他们的包装类,生成的是对象类型(通过new操作符产生的)。

现在有自动装箱拆箱,所以很多时候看不出 int和Integer有什么区别,不过老版本java就很容易看出来。

包装类可以使用方法,基础类型在转换成包装类前不能使用方法。

有好多的方法可以用的,很方便,呵呵