深入理解Java之裝箱與拆箱
一、Java數(shù)據(jù)類型
1、在說裝箱與拆箱之前,先說一下Java的基本數(shù)據(jù)類型,Java從數(shù)據(jù)類型上可以劃分為值類型與引用類型,值類型是四類八種,分別是:
- 整數(shù)型:byte̵,short̵,int̵,long
- 浮點(diǎn)型:float,double
- 字符型:char
- 布爾型:boolean
數(shù)據(jù)類型 | 內(nèi)存 | 默認(rèn)值 | 包裝類 |
---|---|---|---|
byte | 8位 | 0 | Byte |
short | 16位 | 0 | short |
int | 32位 | 0 | Integer |
long | 64位 | 0L或0l | Long |
float | 32位 | 0.0F或0.0f | Float |
double | 64位 | 0.0D或0.0d | Double |
char | 16位 | \u0000 | Character |
boolean | 8位 | flase | Boolean |
2、引用類型:
- 數(shù)組
- 類(class)
- 接口(Interface)
- 枚舉(enum)
3、值類型與引用類型的區(qū)別
1. 從概念方面上來說:
- 值類型:變量名指向具體的值
- 引用類型:變量名指向數(shù)據(jù)對(duì)象的內(nèi)存地址
2. 從內(nèi)存構(gòu)建方面上來說:
- 值類型:變量在聲明之后,Java就會(huì)立刻分配給它內(nèi)存空間
- 引用類型:它以特殊的方式(類似C指針)指向?qū)ο髮?shí)體,這類變量聲明時(shí)不會(huì)分配內(nèi)存,只是存儲(chǔ)
3. 從使用方面上來說:
- 值類型:使用時(shí)需要賦具體值,判斷時(shí)用 ” == “號(hào)
- 引用類型:使用時(shí)可以賦null,判斷時(shí)使用 equals 方法
二、Java數(shù)據(jù)類型轉(zhuǎn)換
1、自動(dòng)轉(zhuǎn)換
- 定義:程序在執(zhí)行過程中“悄然”進(jìn)行的轉(zhuǎn)換,不需要用戶提前聲明,一般是從位數(shù)低的類型向位數(shù)高的類型轉(zhuǎn)換
- 優(yōu)先關(guān)系:按從低到高的順序轉(zhuǎn)換。不同類型數(shù)據(jù)間的優(yōu)先 關(guān)系如下:
- 低--------------------------------------------->高
- byte,short,char-> int -> long -> float -> double
- 轉(zhuǎn)換規(guī)則:
運(yùn)算中,不同類型的數(shù)據(jù)先轉(zhuǎn)化為同一類型,然后進(jìn)行運(yùn)算
操作數(shù)1類型 | 操作數(shù)2類型 | 轉(zhuǎn)換后的類型 |
---|---|---|
byte、short、char | int | int |
byte、short、char、int | long | long |
byte、short、char、int、long | float | float |
byte、short、char、int、long、float | double | double |
2、強(qiáng)制轉(zhuǎn)換
- 定義:強(qiáng)制類型轉(zhuǎn)換則必須在代碼中聲明,轉(zhuǎn)換順序不受限制
- 格式:在需要轉(zhuǎn)型的數(shù)據(jù)前加上“( )”,然后在括號(hào)內(nèi)加入需要轉(zhuǎn)化的數(shù)據(jù)類型
- 結(jié)果:精度可能會(huì)丟失,也可能更加精確
- int x;
- double y;
- x = (int)3.14 + (int)5.20 //精度丟失
- y = (double)x + (double)8 //精度提升
- 輸出:x = 8;y = 16.0
三、Java之裝箱與拆箱
1、包裝類
- Java是面向?qū)ο笳Z言,號(hào)稱萬事萬物皆對(duì)象,因此,8種基本數(shù)據(jù)類型有了對(duì)應(yīng)的類,這就是包裝類
2、什么是裝箱與拆箱
- 裝箱:將值類型裝換成引用類型的過程
- 拆箱:將引用類型轉(zhuǎn)換成值類型的過程
- 自動(dòng)裝箱:
- int x = 3;
- Integer y = x; //int --> Integer,Integer y = x <==> Integer y = Integer.valueOf(x)
- 自動(dòng)拆箱:
- Integer x = new Integer(5);
- int y = x; //Integer --> int,int y = x <==> int y = x.intValue()
3、裝箱和拆箱是如何實(shí)現(xiàn)的
- 裝箱過程是通過調(diào)用包裝器的valueOf方法實(shí)現(xiàn)的
- 拆箱過程是通過調(diào)用包裝器的 xxxValue方法實(shí)現(xiàn)的。(xxx代表對(duì)應(yīng)的基本數(shù)據(jù)類型)
4、注意點(diǎn):
- 大量使用自動(dòng)拆裝箱會(huì)使性能降低,還會(huì)造成大量的內(nèi)存消耗
- 在重載方法中,可能出現(xiàn)問題
- List<Integer> list = new ArrayList<>();
- Integer x,y,z;
- x = 1;y = 2;z = 4;
- list.add(x);list.add(y);list.add(z);
- list.remove(2);
在上面這段代碼中ArrayList.remove方法有兩個(gè)重載方法,那么list.remove(2)是調(diào)用了哪個(gè)方法,remove掉的是值為2的對(duì)象,還是remove了index為2,值為4的那個(gè)對(duì)象呢?
在這種情況下,編譯器不會(huì)進(jìn)行自動(dòng)拆裝箱,所以調(diào)用的是remove(int index),index為2值為4的這個(gè)Integer對(duì)象會(huì)被remove.
如果要調(diào)用 remove(Object o)的方法,應(yīng)該這么寫 list.remove(y)
3. 緩存值問題
- 案例解析:
- Integer i1 = 100;
- Integer i2 = 100;
- Integer i3 = 200;
- Integer i4 = 200;
- System.out.println(i1==i2);
- System.out.println(i3==i4);
- Output: true false
- 觀察源碼:
Intteger.valueOf方法
- public static Integer valueOf(int i) {
- if (i >= IntegerCache.low && i <= IntegerCache.high)
- return IntegerCache.cache[i + (-IntegerCache.low)];
- return new Integer(i);
- }
IntegerCache類
- private static class IntegerCache {
- static final int low = -128;
- static final int high;
- static final Integer cache[];
- static {
- // high value may be configured by property
- int h = 127;
- String integerCacheHighPropValue =
- sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
- if (integerCacheHighPropValue != null) {
- try {
- int i = parseInt(integerCacheHighPropValue);
- i = Math.max(i, 127);
- // Maximum array size is Integer.MAX_VALUE
- h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
- } catch( NumberFormatException nfe) {
- // If the property cannot be parsed into an int, ignore it.
- }
- }
- hhigh = h;
- cache = new Integer[(high - low) + 1];
- int j = low;
- for(int k = 0; k < cache.length; k++)
- cache[k] = new Integer(j++);
- // range [-128, 127] must be interned (JLS7 5.1.7)
- assert IntegerCache.high >= 127;
- }
- private IntegerCache() {}
- }
從源碼可以看出,在通過valueOf方法創(chuàng)建Integer對(duì)象的時(shí)候,如果數(shù)值在[-128,127]之間,便返回指向IntegerCache.cache中已經(jīng)存在的對(duì)象的引用;否則創(chuàng)建一個(gè)新的Integer對(duì)象
- Byte、Short、Integer、Long四種包裝類默認(rèn)創(chuàng)建了數(shù)值為[-128,127]的相應(yīng)類型的緩存數(shù)據(jù),但是超出此范圍仍會(huì)創(chuàng)建新的對(duì)象。
- Character默認(rèn)會(huì)創(chuàng)建[0,127]的響應(yīng)類型的緩存數(shù)據(jù)
- 兩種浮點(diǎn)型沒有實(shí)現(xiàn)常量池技術(shù),在某個(gè)范圍內(nèi)的整型數(shù)值的個(gè)數(shù)是有限的,而浮點(diǎn)數(shù)卻不是
包裝類 | 常量池 | 常量池范圍 |
---|---|---|
Byte | 存在 | [-128,127] |
Short | 存在 | [-128,127] |
Integer | 存在 | [-128,127] |
Long | 存在 | [-128,127] |
Character | 存在 | [0,127] |
Float | 不存在 | 無 |
Double | 不存在 | 無 |
- 注意點(diǎn):
- 當(dāng) "=="運(yùn)算符的兩個(gè)操作數(shù)都是 包裝器類型的引用,則是比較指向的是否是同一個(gè)對(duì)象,而如果其中有一個(gè)操作數(shù)是表達(dá)式(即包含算術(shù)運(yùn)算)則比較的是數(shù)值(即會(huì)觸發(fā)自動(dòng)拆箱的過程)
- 對(duì)于包裝器類型,equals方法并不會(huì)進(jìn)行類型轉(zhuǎn)換
- 算術(shù)運(yùn)算會(huì)觸發(fā)裝箱與拆箱過程