13518219792

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

深入理解Java之装箱与拆箱

一、Java数据类型

10多年的牟平网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网整合营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整牟平建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联公司从事“牟平网站设计”,“牟平网站推广”以来,每个客户项目都认真落实执行。

1、在说装箱与拆箱之前,先说一下Java的基本数据类型,Java从数据类型上可以划分为值类型与引用类型,值类型是四类八种,分别是:

数据类型 内存 默认值 包装类
byte8位0Byte
short16位0short
int32位0Integer
long64位0L或0lLong
float32位0.0F或0.0fFloat
double64位0.0D或0.0dDouble
char16位\u0000Character
boolean8位flaseBoolean

2、引用类型:

3、值类型与引用类型的区别

   1.  从概念方面上来说:

   2.  从内存构建方面上来说:

   3.  从使用方面上来说:

二、Java数据类型转换

1、自动转换

    运算中,不同类型的数据先转化为同一类型,然后进行运算   

操作数1类型 操作数2类型 转换后的类型
byte、short、charintint
byte、short、char、intlonglong
byte、short、char、int、longfloatfloat
byte、short、char、int、long、floatdoubledouble

2、强制转换

 
 
 
 
  1. int x;  
  2.  double y;  
  3.  x = (int)3.14 + (int)5.20  //精度丢失  
  4.  y = (double)x + (double)8  //精度提升  
  5.  输出:x = 8;y = 16.0 

三、Java之装箱与拆箱

1、包装类

2、什么是装箱与拆箱

 
 
 
 
  1. int x = 3;  
  2.    Integer y = x;  //int --> Integer,Integer y = x <==> Integer y = Integer.valueOf(x) 
 
 
 
 
  1. Integer x = new Integer(5);  
  2.    int y = x;  //Integer --> int,int y = x <==> int y = x.intValue() 

3、装箱和拆箱是如何实现的

4、注意点:

  1.  大量使用自动拆装箱会使性能降低,还会造成大量的内存消耗
  2.  在重载方法中,可能出现问题 
 
 
 
 
  1. List list = new ArrayList<>();  
  2.   Integer x,y,z;  
  3.   x = 1;y = 2;z = 4;  
  4.   list.add(x);list.add(y);list.add(z);  
  5.   list.remove(2); 

在上面这段代码中ArrayList.remove方法有两个重载方法,那么list.remove(2)是调用了哪个方法,remove掉的是值为2的对象,还是remove了index为2,值为4的那个对象呢?

在这种情况下,编译器不会进行自动拆装箱,所以调用的是remove(int index),index为2值为4的这个Integer对象会被remove.

如果要调用 remove(Object o)的方法,应该这么写 list.remove(y)

    3.  缓存值问题

 
 
 
 
  1. Integer i1 = 100;  
  2.    Integer i2 = 100;  
  3.    Integer i3 = 200;  
  4.    Integer i4 = 200;  
  5.    System.out.println(i1==i2);  
  6.    System.out.println(i3==i4);  
  7.    Output: true false 

    Intteger.valueOf方法   

 
 
 
 
  1. public static Integer valueOf(int i) {  
  2.         if (i >= IntegerCache.low && i <= IntegerCache.high)  
  3.             return IntegerCache.cache[i + (-IntegerCache.low)];  
  4.         return new Integer(i);  
  5.     } 

IntegerCache类   

 
 
 
 
  1. private static class IntegerCache { 
  2.        static final int low = -128;  
  3.        static final int high;  
  4.        static final Integer cache[];  
  5.        static {  
  6.            // high value may be configured by property  
  7.            int h = 127;  
  8.            String integerCacheHighPropValue =  
  9.                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");  
  10.            if (integerCacheHighPropValue != null) {  
  11.                try {  
  12.                    int i = parseInt(integerCacheHighPropValue);  
  13.                    i = Math.max(i, 127);  
  14.                    // Maximum array size is Integer.MAX_VALUE  
  15.                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1);  
  16.                } catch( NumberFormatException nfe) {  
  17.                    // If the property cannot be parsed into an int, ignore it.  
  18.                }  
  19.            }  
  20.            hhigh = h;  
  21.            cache = new Integer[(high - low) + 1];  
  22.            int j = low;  
  23.            for(int k = 0; k < cache.length; k++)  
  24.                cache[k] = new Integer(j++);  
  25.            // range [-128, 127] must be interned (JLS7 5.1.7)  
  26.            assert IntegerCache.high >= 127;  
  27.        }  
  28.        private IntegerCache() {}  
  29.    } 

从源码可以看出,在通过valueOf方法创建Integer对象的时候,如果数值在[-128,127]之间,便返回指向IntegerCache.cache中已经存在的对象的引用;否则创建一个新的Integer对象

包装类 常量池 常量池范围
Byte存在[-128,127]
Short存在[-128,127]
Integer存在[-128,127]
Long存在[-128,127]
Character存在[0,127]
Float不存在
Double不存在

标题名称:深入理解Java之装箱与拆箱
本文地址:http://cdbrznjsb.com/article/dpojihi.html

其他资讯

让你的专属顾问为你服务