Java基礎(chǔ)教程之基本類(lèi)型數(shù)據(jù)類(lèi)型、包裝類(lèi)及自動(dòng)拆裝箱
前言
我們知道基本數(shù)據(jù)類(lèi)型包括byte, short, int, long, float, double, char, boolean,對(duì)應(yīng)的包裝類(lèi)分別是Byte, Short, Integer, Long, Float, Double, Character, Boolean。關(guān)于基本數(shù)據(jù)類(lèi)型的介紹可參考Java基礎(chǔ)(一) 八大基本數(shù)據(jù)類(lèi)型
那么為什么需要包裝類(lèi)?
JAVA是面向?qū)ο蟮恼Z(yǔ)言,很多類(lèi)和方法中的參數(shù)都需使用對(duì)象,但基本數(shù)據(jù)類(lèi)型卻不是面向?qū)ο蟮模@就造成了很多不便。
如:List<int> = new ArrayList<>();
,就無(wú)法編譯通過(guò)
為了解決該問(wèn)題,我們引入了包裝類(lèi),顧名思義,就是將基本類(lèi)型“包裝起來(lái)“,使其具備對(duì)象的性質(zhì),包括可以添加屬性和方法,位于java.lang包下。
拆箱與裝箱
既然有了基本數(shù)據(jù)類(lèi)型和包裝類(lèi),就必然存在它們之間的轉(zhuǎn)換,如:
public static void main(String[] args) { Integer a = 0; for(int i = 0; i < 100; i++){ a += i; } }
將基本數(shù)據(jù)類(lèi)型轉(zhuǎn)為包裝類(lèi)的過(guò)程叫“裝箱”;
將包裝類(lèi)轉(zhuǎn)為基本數(shù)據(jù)類(lèi)型的過(guò)程叫“拆箱”;
自動(dòng)拆箱與自動(dòng)裝箱
Java為了簡(jiǎn)便拆箱與裝箱的操作,提供了自動(dòng)拆裝箱的功能,這極大地方便了程序員們。那么到底是如何實(shí)現(xiàn)的呢?
上面的例子就是一個(gè)自動(dòng)拆箱與裝箱的過(guò)程,通過(guò)反編譯工具我們得到,
public static void main(String[] args) { Integer a = Integer.valueOf(0); for (int i = 0; i < 100; i++) { a = Integer.valueOf(a.intValue() + i); } }
我們不難發(fā)現(xiàn),主要調(diào)用了兩個(gè)方法,Integer.intValue() 和 Integer.valueOf( int i) 方法
查看Integer源碼,我們找到了對(duì)應(yīng)代碼:
/** * Returns the value of this {@code Integer} as an * {@code int}. */ public int intValue() { return value; }
/** * 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); }
很明顯,我們我們得出,Java幫你隱藏了內(nèi)部細(xì)節(jié)。
拆箱的過(guò)程就是通過(guò)Integer 實(shí)體調(diào)用intValue()方法;
裝箱的過(guò)程就是調(diào)用了 Integer.valueOf(int i) 方法,幫你直接new了一個(gè)Integer對(duì)象
那么哪些地方會(huì)進(jìn)行自動(dòng)拆裝箱?
其實(shí)很簡(jiǎn)單
1.添加到集合中時(shí),進(jìn)行自動(dòng)裝箱
2.涉及到運(yùn)算的時(shí)候,“加,減,乘, 除” 以及 “比較 equals,compareTo”,進(jìn)行自動(dòng)拆箱
注意的點(diǎn)
在上述的代碼中,關(guān)于Integer valueOf(int i)方法中有IntegerCache類(lèi),在自動(dòng)裝箱的過(guò)程中有個(gè)條件判斷
if (i >= IntegerCache.low && i <= IntegerCache.high)
結(jié)合注釋
This method will always cache values in the range -128 to 127,
inclusive, and may cache other values outside of this range.
大意是:該方法總是緩存-128 到 127之間的值,同時(shí)針對(duì)超出這個(gè)范圍的值也是可能緩存的。
那么為什么可能緩存?其實(shí)在IntegerCache源碼中可以得到答案
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. } }
因?yàn)榫彺孀畲笾凳强梢耘渲玫?。這樣設(shè)計(jì)有一定好處,我們可以根據(jù)應(yīng)用程序的實(shí)際情況靈活地調(diào)整來(lái)提高性能。
與之類(lèi)似還有:
Byte與ByteCache,緩存值范圍-128到127,固定不可配置
Short與ShortCache,緩存值范圍-128到127,固定不可配置
Long與LongCache,緩存值范圍-128到127,固定不可配置
Character與CharacterCache,緩存值范圍0到127,固定不可配置
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Java并發(fā)編程如何降低鎖粒度并實(shí)現(xiàn)性能優(yōu)化
這篇文章主要介紹了Java并發(fā)編程如何降低鎖粒度并實(shí)現(xiàn)性能優(yōu)化,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08Springboot 如何設(shè)置啟動(dòng)內(nèi)存
這篇文章主要介紹了Springboot 如何設(shè)置啟動(dòng)內(nèi)存,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02springboot如何實(shí)現(xiàn)異步響應(yīng)請(qǐng)求(前端請(qǐng)求超時(shí)的問(wèn)題解決)
這篇文章主要給大家介紹了關(guān)于springboot如何實(shí)現(xiàn)異步響應(yīng)請(qǐng)求(前端請(qǐng)求超時(shí)的問(wèn)題解決)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用springboot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-01-01java中xml和對(duì)象之間的互相轉(zhuǎn)換方法
在java開(kāi)發(fā)中我們經(jīng)常會(huì)遇到Xml與對(duì)象互相轉(zhuǎn)換的情況,這篇文章主要給大家介紹了關(guān)于java中xml和對(duì)象之間的互相轉(zhuǎn)換方法,文中給出了兩種解決方法,需要的朋友可以參考下2023-06-06