Java詳細分析講解自動裝箱自動拆箱與Integer緩存的使用
1. 前言
自動裝箱和自動拆箱是什么?Integer緩存是什么?它們之間有什么關(guān)系?
先來看一道題目。
Integer a = new Integer(1); Integer b = new Integer(1); System.out.println(a==b); Integer c = 1; Integer d = 1; System.out.println(c==d); Integer e = 128; Integer f = 128; System.out.println(e==f);
先答,后看答案。
答案是false true false,你答對了嗎?
既然一塊出現(xiàn)了,就一起串一下知識點
2. 包裝類
Java中基本數(shù)據(jù)類型有八種,可以分為三類:
- 字符型:char
- 布爾型:boolean
- 數(shù)值型:byte short int long float double
包裝類是將八種基本數(shù)據(jù)類型包裝為了類,使它們可以使用Java的三大特性:封裝、繼承、多態(tài)。對應(yīng)關(guān)系如下:
基本數(shù)據(jù)類型 | 對應(yīng)的包裝類 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
char | Character |
數(shù)值型對應(yīng)的六個包裝類都繼承于Number類。
3. 自動裝箱與自動拆箱
八種基本數(shù)據(jù)類型對應(yīng)八種包裝類,那么它們是怎么進行數(shù)據(jù)轉(zhuǎn)換的?
//基本數(shù)據(jù)類型轉(zhuǎn)包裝類 //1.有參構(gòu)造 Integer a = new Integer(1); //2.實際上,有參構(gòu)造的參數(shù)也可以是字符串,不過要使用正確的數(shù)據(jù),“123abc”不可能會轉(zhuǎn)換為Integer類型 Integer b = new Integer("123"); //3.valueOf() Integer c = Integer.valueOf(123); //包裝類轉(zhuǎn)基本數(shù)據(jù)類型(xxxValue() float是floatValue() double是doubleValue()) int d = a.intValue();
以上的形式都是比較符合認知的,獲取一個對象可以通過new或者調(diào)用某個方法,獲取一個值就調(diào)用對象的某個屬性。
Java 5.0之后可以不用這么麻煩了,增加了自動裝箱和自動拆箱的新特性,實際上兩個概念非常好理解。
int a = 10; Integer b = a; //自動裝箱 int c = b; //自動拆箱
乍一看,對象=數(shù)值的這種形式并不符合認知,但是借助于自動裝箱和自動拆箱就可以實現(xiàn)。實際上,編譯器還是借助于valueOf()和xxxValue()實現(xiàn)的。
我們來看一下valueOf()源碼。
/** * Returns an {@code Integer} instance representing the specified * {@code int} value. If a new {@code Integer} instance is not * required, this method should generally be used in preference to * the constructor {@link #Integer(int)}, as this method is likely * to yield significantly better space and time performance by * caching frequently requested values. * * This method will always cache values in the range -128 to 127, * inclusive, and may cache other values outside of this range. * * @param i an {@code int} value. * @return an {@code Integer} instance representing {@code i}. * @since 1.5 */ public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
valueOf()并不是簡單地返回Integer對象,而是先進行了一次判斷,輸入的數(shù)據(jù)符合某個范圍的話,將會返回一個特定的對象,從注釋上來看,這個范圍默認是[-128,127],并且可能是更大的范圍;而超過這個范圍就會返回new的對象。而使用到的IntegerCache數(shù)據(jù)就是Integer的緩存了。
4. Interger緩存
數(shù)值計算日常使用比較頻繁,那如果不停的去new Integer對象的話,開銷會非常大,所以,Java在執(zhí)行程序時會自動生成一個靜態(tài)數(shù)組作為緩存,Integer默認對應(yīng)的緩存數(shù)組范圍在[-128,127],只要數(shù)據(jù)在這個范圍內(nèi),就可以從緩存中拿到相應(yīng)的對象。
看一下IntegerCache源碼。
/** * Cache to support the object identity semantics of autoboxing for values between * -128 and 127 (inclusive) as required by JLS. * * The cache is initialized on first usage. The size of the cache * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option. * During VM initialization, java.lang.Integer.IntegerCache.high property * may be set and saved in the private system properties in the * sun.misc.VM class. */ 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. } } high = 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() {} }
可以看到,IntegerCache是Integer的靜態(tài)內(nèi)部類,valueOf()調(diào)用的IntegerCache.cache就是一個數(shù)組對象,數(shù)組的大小取決于范圍內(nèi)的最大值和最小值,默認是[-128,127],當然,(注釋上說)也可以通過JVM修改這個范圍(這我不了解)。然后數(shù)組內(nèi)的元素都會被賦一個Integer對象,緩存也就形成了。
存在數(shù)組緩存,也就意味著,如果取值在[-128,127],使用valueOf()或者自動裝箱創(chuàng)建的Integer對象都是在數(shù)組中取出,因此對象指向的內(nèi)存地址是完全一樣的。而如果用new或者是超出這個范圍都要重新創(chuàng)建對象。
當然,不止Integer有緩存機制,Byte、Short、Long、Character都具有緩存機制。其中Byte,Short,Integer,Long的范圍為 -128 到 127,Character的范圍為 0 到 127。
5. 回答題目
Integer a = new Integer(1); Integer b = new Integer(1); System.out.println(a==b); Integer c = 1; Integer d = 1; System.out.println(c==d); Integer e = 128; Integer f = 128; System.out.println(e==f);
1.new創(chuàng)建的兩個對象,即使值相同,指向的內(nèi)存地址也是不同的,使用==進行比較返回結(jié)果為false
2.自動裝箱和緩存機制,兩個對象實際上是相同的,返回結(jié)果為true
3.超出緩存范圍,執(zhí)行時會new新對象,兩個對象不同,返回結(jié)果為false
到此這篇關(guān)于Java詳細分析講解自動裝箱自動拆箱與Integer緩存的使用的文章就介紹到這了,更多相關(guān)Java自動裝箱內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java設(shè)計模式之代理模式_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了Java設(shè)計模式之代理模式,本文詳細的介紹了什么事代理模式和相關(guān)的類和接口,有興趣的可以了解一下2017-08-08淺談Hibernate對象狀態(tài)之間的神奇轉(zhuǎn)換
這篇文章主要介紹了淺談Hibernate對象狀態(tài)之間的神奇轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Spring MVC的優(yōu)點與核心接口_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了Spring MVC的優(yōu)點與核心接口,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08Feign調(diào)用服務(wù)時丟失Cookie和Header信息的解決方案
這篇文章主要介紹了Feign調(diào)用服務(wù)時丟失Cookie和Header信息的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03