国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

java中int和Integer的理解

2019-11-06 06:34:34
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

前言

java中的數(shù)據(jù)類(lèi)型分為基本數(shù)據(jù)類(lèi)型和復(fù)雜數(shù)據(jù)類(lèi)型.int是基本數(shù)據(jù)類(lèi)型,Integer是復(fù)雜數(shù)據(jù)類(lèi)型.所以int的默認(rèn)值為0,而Integer的默認(rèn)值為null,即Integer可以區(qū)分出未賦值和值為0的區(qū)別,int則無(wú)法表達(dá)出未賦值的情況。


java中為什么要引入Integer?

個(gè)人認(rèn)為很多java api的調(diào)用包括方法的傳旨或者范型的使用都是基于Object的,所以引入了Integer,引入了自動(dòng)裝箱與拆箱,這些方便api的調(diào)用.


java中的自動(dòng)裝箱與拆箱

自動(dòng)裝箱:

Integer i = 100;相當(dāng)于編譯器自動(dòng)為您作以下的語(yǔ)法編譯:Integer i = Integer.valueOf(100);換句話說(shuō),裝箱就是jdk自己幫你完成了調(diào)用Integer.valueOf(100)。

Integer類(lèi)的valueOf(int i)方法實(shí)現(xiàn)

/** * Returns a <tt>Integer</tt> instance rePResenting the specified * <tt>int</tt> value. * If a new <tt>Integer</tt> 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. * * @param i an <code>int</code> value. * @return a <tt>Integer</tt> instance representing <tt>i</tt>. * @since 1.5 */ public static Integer valueOf(int i) { if(i >= -128 && i <= IntegerCache.high) return IntegerCache.cache[i + 128]; else return new Integer(i); }

自動(dòng)拆箱: 自動(dòng)拆箱(unboxing),也就是將對(duì)象中的基本數(shù)據(jù)從對(duì)象中自動(dòng)取出:

Integer i = 10; //裝箱 int m = i; //拆箱,實(shí)際上執(zhí)行了 int m = i.intValue();

下面的測(cè)試是針對(duì)int和Integer的區(qū)別分析

@Test public void testInteger(){ //會(huì)把Integer自動(dòng)拆箱為 int再去比,等同于Integer in=10; Integer in=new Integer(10); int i=10; System.out.println(i==in);//true Integer i1=new Integer(20); Integer i2=20; System.out.println(i1==i2);//false Integer i3=new Integer(30); Integer i4=new Integer(30); System.out.println(i3==i4);//false Integer i5=40; Integer i6=40; System.out.println(i5==i6);//true System.out.println("================"); Integer i7=-127; Integer i8=-127; System.out.println(i7==i8);//true Integer i9=127; Integer i10=127; System.out.println(i9==i10);//true Integer i11=128; Integer i12=128; System.out.println(i11==i12);//false Integer i13=-128; Integer i14=-128; System.out.println(i13==i14);//true //Integer緩存區(qū)間[-128,127] 超出Integer的緩存范圍,不從私有靜態(tài)內(nèi)部類(lèi)IntegerCache的 數(shù)組cache中獲得,而是通過(guò)new返回新對(duì)象 Integer i131=-129; Integer i132=-129; System.out.println(i131==i132);//false Integer i15=250; Integer i16=250; System.out.println(i15==i16);//false }

Integer源碼片段:

/* @param i an <code>int</code> value. * @return a <tt>Integer</tt> instance representing <tt>i</tt>. * @since 1.5 */ public static Integer valueOf(int i) { //沒(méi)有設(shè)置的話,IngegerCache.high 默認(rèn)是127 if(i >= -128 && i <= IntegerCache.high) return IntegerCache.cache[i + 128]; else return new Integer(i); }對(duì)于–128到127(默認(rèn)是127)之間的值,Integer.valueOf(int i) 返回的是緩存的Integer對(duì)象(并不是新建對(duì)象)而其他值,執(zhí)行Integer.valueOf(int i) 返回的是一個(gè)新建的 Integer對(duì)象private static class IntegerCache { static final int high; static final Integer cache[]; static { final int low = -128; // high value may be configured by property int h = 127; if (integerCacheHighPropValue != null) { // Use Long.decode here to avoid invoking methods that // require Integer's autoboxing cache to be initialized int i = Long.decode(integerCacheHighPropValue).intValue(); i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - -low); } high = h; cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); } private IntegerCache() {} }

看一下源碼大家都會(huì)明白,對(duì)于-128到127之間的數(shù),會(huì)進(jìn)行緩存. IntegerCache有靜態(tài)成員變量cache,為一個(gè)擁有256個(gè)元素的數(shù)組。在IntegerCache中也對(duì)cache進(jìn)行了初始化,即第i個(gè)元素是值為i-128的Integer對(duì)象。而-128至127是最常用的Integer對(duì)象,這樣的做法也在很大程度上提高了性能.


結(jié)論

針對(duì)以上案例分析得出如下結(jié)論:

1)Integer與new Integer不會(huì)相等,他們的內(nèi)存地址不一樣 2)兩個(gè)都是非new出來(lái)的Integer,如果數(shù)在-128到127之間,則是true,否則為false 3)兩個(gè)都是new出來(lái)的,都為false 4)int和integer(無(wú)論new否)比,都為true,因?yàn)闀?huì)把Integer自動(dòng)拆箱為int再去做對(duì)比.

簡(jiǎn)單來(lái)說(shuō)就是:當(dāng)封裝類(lèi)與基礎(chǔ)類(lèi)型進(jìn)行==運(yùn)行時(shí),封裝類(lèi)會(huì)進(jìn)行拆箱,拆箱結(jié)果與基礎(chǔ)類(lèi)型對(duì)比值;而兩個(gè)封裝類(lèi)進(jìn)行==運(yùn)行時(shí),與其它的對(duì)象進(jìn)行==運(yùn)行一樣,對(duì)比兩個(gè)對(duì)象的地址,也即判斷是否兩個(gè)引用是否指向同一個(gè)對(duì)象。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 景洪市| 水城县| 蓝山县| 扶沟县| 始兴县| 仪征市| 土默特右旗| 永仁县| 康马县| 长阳| 仲巴县| 屯昌县| 永德县| 淳安县| 大荔县| 文成县| 闽侯县| 沁源县| 来凤县| 万源市| 丘北县| 清水县| 建昌县| 沈丘县| 临沭县| 焦作市| 诸城市| 卢氏县| 三明市| 南江县| 绥中县| 曲松县| 平乐县| 牟定县| 大化| 张掖市| 靖边县| 应城市| 江西省| 年辖:市辖区| 芦溪县|