Java自動(dòng)拆裝箱簡(jiǎn)單介紹
在面試過(guò)程中,常常會(huì)有面試官問(wèn)到基礎(chǔ)的問(wèn)題的時(shí)候都會(huì)問(wèn)到Java的拆裝箱,關(guān)于這個(gè)問(wèn)題其實(shí)不是很難,但是如果平時(shí)進(jìn)行自學(xué)的時(shí)候不是注意,就可能一臉懵逼,所以筆者就這個(gè)問(wèn)題進(jìn)行一些總結(jié),共同促進(jìn)!
一、拆裝箱概念
所謂的拆裝箱,就是自從JDK1.5之后,java的基本類(lèi)型和引用類(lèi)型之間的相互轉(zhuǎn)換。
1.1拆箱
拆箱就是把Long,Integer,Double,F(xiàn)loat 等將基本數(shù)據(jù)類(lèi)型的首字母大寫(xiě)的相應(yīng)的引用類(lèi)型轉(zhuǎn)化為基本數(shù)據(jù)類(lèi)型的動(dòng)作就叫拆箱。
1.2裝箱
裝箱就是把byte ,int ,short, long ,double,float,boolean,char 這些Java的基本數(shù)據(jù)類(lèi)型在定義數(shù)據(jù)類(lèi)型時(shí)不聲明為相對(duì)應(yīng)的引用類(lèi)型,在編譯器的處理下自動(dòng)轉(zhuǎn)化為引用類(lèi)型的動(dòng)作就叫做裝箱。
二、拆裝箱的相關(guān)應(yīng)用
在JDK1.5后,當(dāng)我們進(jìn)行基本類(lèi)型和引用類(lèi)型的轉(zhuǎn)換的時(shí)候就會(huì)方便:
package com.hzp.CZX; /** * 測(cè)試拆裝箱 * @author 夜孤寒 * @version 1.1.1 */ public class TestDemo { /** * 拆裝箱JDK1.5后 */ public static void first(){ Integer i=7;//基本類(lèi)型-->引用類(lèi)型 int j=i;//引用類(lèi)型-->基本類(lèi)型 System.out.println(j); } /** * 拆裝箱JDK1.4 */ public static void second(){ Integer i=new Integer(78); int j=i.intValue(); System.out.println(j); } /** * 測(cè)試方法 * @param args */ public static void main(String[] args) { first(); second(); } }
上面介紹了關(guān)于拆裝箱的一些基本點(diǎn)和使用方式,但是要使用拆裝箱的話還有一些注意點(diǎn)需要注意,下面將這些注意點(diǎn)進(jìn)行一些總結(jié)。
三、注意點(diǎn)
首先貼一段代碼如下:
package com.ygh.CZX; /** * 關(guān)于java的拆裝箱范圍剖析 * @author 夜孤寒 * @version 1.1.1 */ public class Test { /** * 以Integer類(lèi)型為例 */ public static void first(){ Integer i=new Integer(124); Integer j=new Integer(124); System.out.println(i==j);//false Integer a1=-128; Integer a2=-128; System.out.println(a1==a2);//true Integer b1=-129; Integer b2=-129; System.out.println(b1==b2);//false Integer c1=127; Integer c2=127; System.out.println(c1==c2);//true Integer d1=128; Integer d2=128; System.out.println(d1==d2);//false } public static void main(String[] args) { first(); } }
簡(jiǎn)單解釋一下:
第一個(gè)結(jié)果為false的原因是因?yàn)閯?chuàng)建了不同的對(duì)象,所以?xún)烧卟灰粯樱?/p>
但是第二個(gè)和第三個(gè)的結(jié)果為什么不一樣?
下面貼出關(guān)于Integer類(lèi)的源碼,從源碼的角度來(lái)分析這個(gè)問(wèn)題:
/** * 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); }
上面的代碼是說(shuō),進(jìn)行自動(dòng)拆裝箱的時(shí)候,是有一個(gè)范圍的,一旦超出這個(gè)范圍,那么指向的就不是同一個(gè)對(duì)象,而是返回一個(gè)新創(chuàng)建的對(duì)象了,這個(gè)范圍在Integer類(lèi)中的一個(gè)內(nèi)部私有類(lèi)IntegerCache可以體現(xiàn)出來(lái),源碼如下:
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() {} }
從這里我們可以看出,范圍值為[-128,127]之間。
注意,Integer、Short、Byte、Character、Long這幾個(gè)類(lèi)的valueOf方法的實(shí)現(xiàn)是類(lèi)似的。
Double、Float的valueOf方法的實(shí)現(xiàn)是類(lèi)似的。
總結(jié):這些進(jìn)行自動(dòng)拆裝箱的基本類(lèi)型的范圍如下:
1. boolean類(lèi)型的值
2.所有的byte的值
3.在-128~127的short類(lèi)型的值
4.在-128~127的int類(lèi)型的值
5.在\ u0000~\ u00ff 之間的char類(lèi)型的值
而其中double和float又有所不同,我們就以double為例子,貼出代碼討論:
package com.ygh.CZX; /** * 關(guān)于java的拆裝箱范圍剖析 * * @author 夜孤寒 * @version 1.1.1 */ public class Test { /** * Double */ public static void first() { Double i1 = 100.0; Double i2 = 100.0; Double i3 = 200.0; Double i4 = 200.0; System.out.println(i1 == i2);//false System.out.println(i3 == i4);//false } /** * 測(cè)試方法 */ public static void main(String[] args) { first(); } }
注意為什么上面的代碼的輸出結(jié)果都是false呢?同樣的我們依舊以Double類(lèi)中的valueOf方法來(lái)討論,貼出源碼就一目了然了:
/** * Returns a {@code Double} instance representing the specified * {@code double} value. * If a new {@code Double} instance is not required, this method * should generally be used in preference to the constructor * {@link #Double(double)}, as this method is likely to yield * significantly better space and time performance by caching * frequently requested values. * * @param d a double value. * @return a {@code Double} instance representing {@code d}. * @since 1.5 */ public static Double valueOf(double d) { return new Double(d); }
也就是說(shuō)不管你的double是什么范圍的值,他都是給你返回一個(gè)新的對(duì)象。float同double,就不過(guò)多贅述了。
以上就是筆者對(duì)于拆裝箱的一些整理,如果讀者有不同的看法可以在評(píng)論區(qū)提出,筆者再進(jìn)行修改!
希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java字符串遍歷以及統(tǒng)計(jì)字符串中各類(lèi)字符
這篇文章主要為大家詳細(xì)介紹了java字符串遍歷以及字符串中各類(lèi)字符統(tǒng)計(jì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03java8?Stream大數(shù)據(jù)量List分批處理切割方式
這篇文章主要介紹了java8?Stream大數(shù)據(jù)量List分批處理切割方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02Quarkus中的依賴(lài)注入DI和面向切面aop編程
這篇文章主要為大家介紹了Quarkus中的依賴(lài)注入DI和面向切面aop的編程規(guī)范思想,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02Spring?Boot?集成Redisson實(shí)現(xiàn)分布式鎖詳細(xì)案例
這篇文章主要介紹了Spring?Boot?集成Redisson實(shí)現(xiàn)分布式鎖詳細(xì)案例,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08Spring解讀@Component和@Configuration的區(qū)別以及源碼分析
通過(guò)實(shí)例分析@Component和@Configuration注解的區(qū)別,核心在于@Configuration會(huì)通過(guò)CGLIB代理確保Bean的單例,而@Component不會(huì),在Spring容器中,使用@Configuration注解的類(lèi)會(huì)被CGLIB增強(qiáng),保證了即使在同一個(gè)類(lèi)中多次調(diào)用@Bean方法2024-10-10Mybatis中如何設(shè)置sqlSession自動(dòng)提交
在MyBatis中,默認(rèn)情況下,獲取的SqlSession對(duì)象不會(huì)自動(dòng)提交事務(wù),這意味著在進(jìn)行更新、刪除或插入等操作后,需要顯式調(diào)用commit方法來(lái)提交事務(wù),但是,可以在獲取SqlSession時(shí)通過(guò)將openSession方法的參數(shù)設(shè)置為true2024-09-09Spring Boot整合郵件發(fā)送與注意事項(xiàng)
這篇文章主要給大家介紹了關(guān)于Spring Boot整合郵件發(fā)送與注意事項(xiàng)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07Java 并發(fā)編程學(xué)習(xí)筆記之Synchronized底層優(yōu)化
這篇文章主要介紹了Java 并發(fā)編程學(xué)習(xí)筆記之Synchronized底層優(yōu)化的相關(guān)資料,主要包含了重量級(jí)鎖,輕量級(jí)鎖,偏向鎖和其他優(yōu)化等方面,有需要的小伙伴可以參考下2016-05-05