Java中常用包装类

基本数据类型

包装类

byte

Byte

boolean

Boolean

short

Short

char

Character

int

Integer

long

Long

float

Float

double

Double

 

1.1.2为什么需要包装类(优点)

  1. 某些方法的参数必须是对象,为了让基本数据类型的数据能作为参数,提供包装类。
  2. 包装类还可以提供更多的功能
  3. 其他特别重要的功能:比如可以实现字符串和基本数据类型之间的转换

【示例1】认识包装类

public class TestWrapper1 {
   
int num;
    Integer
in ;
   
public static void main(String[] args) {
       
//1.某些方法参数是对象,为了让基本数据类型能作为参数
       
List list = new ArrayList();
        list.add(
new Integer(56)); //int 56
       
list.add(new Integer(100));
        list.add(
new Double(67.5));
        list.add(
99);
        System.
out.println(list);
       
//2.包装类还可以提供更多的功能
        System.out.println(Integer.SIZE);

        System.out.println(Integer.MIN_VALUE);
        System.
out.println(Integer.MAX_VALUE);    

     System.out.println(Integer.toBinaryString(123));
        System.out.println(Integer.toOctalString(123));
        System.out.println(Integer.toHexString(123));
        //3.特别重要的功能:可以实现字符串和基本数据类型之间的转换
       
String str = "123"; //----123
       
int num2 = Integer.parseInt(str);
        System.
out.println(num2);
        String str2 =
"123.45";
       
double d = Double.parseDouble(str2);
        System.
out.println(d);
    }
}

 

注意

  • 注意1:包装类的对象需要占用栈内存和堆内存,而基本数据类型的(局部)变量只占用内存;基本数据类型的变量占用空间少,更简单,更灵活,更高效。
  • 注意2:作为成员变量,初始值不同。int  0;Integer null。
  • 注意3:在这八个类中,除了Character和Boolean以外,其他的都是“数字型“,“数字型”都是java.lang.Number的子类。

 

自动装箱和自动拆箱

自动装箱和拆箱就是将基本数据类型和包装类之间进行自动的互相转换。JDK1.5后,Java引入了自动装箱(autoboxing)/拆箱(unboxing)

自动装箱:基本类型的数据处于需要对象的环境中时,会自动转为“对象”。

自动拆箱:每当需要一个值时,对象会自动转成基本数据类型,没必要再去显式调用intValue()、doubleValue()等转型方法。

如 Integer i = 5;int j = i; 这样的过程就是自动拆箱。  

我们可以用一句话总结自动装箱/拆箱:

自动装箱过程是通过调用包装类的valueOf()方法实现的,而自动拆箱过程是通过调用包装类的 xxxValue()方法实现的(xxx代表对应的基本数据类型,如intValue()、doubleValue()等)。

【示例2】包装类的使用

public class TestWrapper2 {
   
public static void main(String[] args) {
       
//1.自动装箱和自动拆箱
       
Integer in = 5;
        Integer in2 =
new Integer(5);//valueOf()
       
int i = in2;
       
int i2 = in2.intValue();
       
//2.==  equals
       
Integer in3 = new Integer(56);
        Integer in4 =
new Integer(56);
        System.
out.println(in3==in4); //false
       
System.out.println(in3.equals(in4));//true
       
Integer in5 = 25;
        Integer in6 =
25;
        System.
out.println(in5 ==in6);//true  false ???
       
System.out.println(in5.equals(in6));//true
       
Integer in7 = 256;
        Integer in8 =
256;
        System.
out.println(in7 ==in8);//true  false ???
       
System.out.println(in7.equals(in8));//true
   
}
}

1.2.2理解Integer源码

  • Integer的父类是Number类;底层就是封装了一个int类型的value常量,可以通过构造方法、intValue()等赋值取值。

public class Integer extends Number {
   
private final int value;
   
public Integer(int value) {
       
this.value = value;
    }
   
@Override
   
public int intValue() {
       
return value;
    }
}

  • Integer类提供了一个静态内部类IntegerCache,对于定义一个静态数组cache,长度为256,赋值为-128—127。对于自动装箱时如果是-128—127范围内的数据,直接获取数组的指定值;对于这个范围之外的数据,通过new Integer()重新创建对象。这么做的目的是提高效率。

public class Integer extends Number {   
   
private static class IntegerCache {
       
static final int low = -128;
       
static final int high;
       
static final java.lang.Integer cache[];
       
static {
           
int h = 127;
           
high = h;
           
cache = new java.lang.Integer[(high - low) + 1];
           
int j = low;
           
for(int k = 0; k < cache.length; k++)
               
cache[k] = new java.lang.Integer(j++);
        }
       
private IntegerCache() {}
    }
   
public static java.lang.Integer valueOf(int i) {
       
if (i >= IntegerCache.low && i <= IntegerCache.high)
           
return IntegerCache.cache[i + (-IntegerCache.low)];
       
return new java.lang.Integer(i);
    }
}

 

注意

  1. JDK1.5以后,增加了自动装箱与拆箱功能,如:

    Integer i = 100;  int j = new Integer(100);

  1. 自动装箱调用的是valueOf()方法,而不是new Integer()方法。
  2. 自动拆箱调用的xxxValue()方法。
  3. 包装类在自动装箱时为了提高效率,对于-128~127之间的值会进行缓存处理。超过范围后,对象之间不能再使用==进行数值的比较,而是使用equals方法。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值