Java 数据类型的包装数据类型

什么是包装数据类型

  Java是一个面向对象的编程语言,但基本类型并不具有对象的性质,为了让基本类型也具有对象的特征,就出现了包装类型。

  集合框架里面需要存储对象,不能存储基本数据类型,所以需要存储包装类型

Java里面的包装数据类型

基本类型  包装器类型
boolean    Boolean
char     Character
int      Integer
byte     Byte
short     Short
long     Long
float     Float
double    Double

互相转换

int i1 = 0; 
Integer integer1 = new Integer(i);
Integer integer2 = new Integer(0); 
int i2 = integer2.intValue(); 
Boolean booleanObj1 = new Boolean(false);
boolean baseBool1 = booleanObj.booleanValue();
boolean baseBool2 = false;
Boolean booleanObj2 = new Boolean(baseBool2);

区别

  • 基本数据类型不用new,包装类型需要使用new关键字来在堆中分配存储空间
  • 存储方式及位置不同,基本类型是直接将变量值存储在栈中,包装类型是将对象放在堆中,然后通过引用来使用
  • 初始值不同,基本类型的初始值如int为0,boolean为false,包装类型的初始值为null

 

posted @ 2020-08-02 02:06  陈彦斌  阅读(747)  评论(0编辑  收藏  举报