詳談java中int和Integer的區(qū)別及自動(dòng)裝箱和自動(dòng)拆箱
int和Integer的區(qū)別及自動(dòng)裝箱和自動(dòng)拆箱
1.Integer是int的包裝類,int則是java的一種基本數(shù)據(jù)類型。
2.Integer變量必須實(shí)例化后才能使用,int則不需要。
3.Integer實(shí)際是對(duì)象的引用,當(dāng)new一個(gè)Integer時(shí),實(shí)際上是生成一個(gè)指針指向此對(duì)象;而int則是直接存儲(chǔ)數(shù)據(jù)值。
4.Integer的默認(rèn)值為null,int的默認(rèn)值是0。
5.int等基本類型的變量存儲(chǔ)在棧中。
Integer和int的對(duì)比,如下所示:
//程序如下所示 int a = 10; int b = 10; Integer c = 10; Integer d = 10; Integer e = new Integer(10); Integer f = new Integer(10); Integer g = 1000; Integer h = 1000; System.out.println("int和int比較值:-----" + (a == b)); System.out.println("int和Integer比較值:-----" + (a == c)); System.out.println("Integer和Integer小于128比較值:-----" + (c == d)); System.out.println("Integer和New Integer()比較值:-----" + (c == e)); System.out.println("New Integer()和New Integer()比較值:-----" + (f == e)); System.out.println("Integer和Integer大于127比較值:-----" + (g == h)); System.out.println("int和New Integer()比較值:-----" + (a == e));
結(jié)果如下所示:
int和int比較值:-----true
int和Integer比較值:-----true
Integer和Integer小于128比較值:-----true
Integer和New Integer()比較值:-----false
New Integer()和New Integer()比較值:-----false
Integer和Integer大于127比較值:-----false
int和New Integer()比較值:-----true
(1)int和int比較:
int直接存儲(chǔ)的數(shù)據(jù)值,因此直接比較值即可。
(2)int和Integer的比較:
Integer和int比較的時(shí)候自動(dòng)拆箱,這是比較值即可。
(3)Integer和Integer的比較:
Integer是包裝類型,是Object對(duì)象,因此==比較的是Integer指向的內(nèi)存地址。然而-128~127直接的Integer數(shù)據(jù)直接緩存進(jìn)入常量池,所以這個(gè)區(qū)間的比較返回true,其他區(qū)間返回false。當(dāng)然,new的Integer對(duì)象不適用。
自動(dòng)裝箱和自動(dòng)拆箱:
基本數(shù)據(jù)類型包括byte,char,short,int,long,float,double,boolean,對(duì)應(yīng)的包裝類型有Byte,Character,Short,Integer,Long,F(xiàn)loat,Double,Boolean類型?;绢愋妥?yōu)榘b類型就是自動(dòng)裝箱,反之就是自動(dòng)拆箱,下面是具體場(chǎng)景。
Integer i = 100; //自動(dòng)裝箱,類似于Integer i = Integer.valueOf(100); int j = i; //自動(dòng)拆箱,類似于int j = i.intValue();
Integer的自動(dòng)拆裝箱的陷阱(整型數(shù)-128到127的值比較問(wèn)題)
Integer的自動(dòng)拆裝箱的陷阱(整型數(shù)-128到127的值比較問(wèn)題):
1、先看下面的例子:
package integerdemo; public class IntegerDemo { public static void main(String[] args) { //-128--127之間 Integer i1 = 100; Integer i2 = 100; if( i1 == i2){ System.out.println("i1 == i2"); }else{ System.out.println("i1 != i2 "); } //大于127 Integer i3 = 200; Integer i4 = 200; if( i3 == i4){ System.out.println("i3 == i4"); }else{ System.out.println("i3 != i4 "); } } }
運(yùn)行結(jié)果:
run:
i1 == i2
i3 != i4
成功構(gòu)建 (總時(shí)間: 1 秒)
以上是靠整型數(shù)的自動(dòng)拆裝箱實(shí)現(xiàn)的,而兩者的結(jié)果卻不相同。
原因在于,在進(jìn)行自動(dòng)拆裝箱時(shí),編譯器會(huì)使用Integer.valueof()來(lái)創(chuàng)建Integer實(shí)例。
2、以下是Integer.valueof()的源代碼:
public static Integer valueOf(int i) { assert IntegerCache.high >= 127; if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
簡(jiǎn)單地解釋這段代碼,就是如果傳入的int在IntegerCache.low和IntegerCache.high之間,那就嘗試看前面的緩存中有沒(méi)有打過(guò)包的相同的值,如果有就直接返回,否則就創(chuàng)建一個(gè)Integer實(shí)例。IntegerCache.low 默認(rèn)是-128;IntegerCache.high默認(rèn)是127.
注:如果要比較兩個(gè)對(duì)象的內(nèi)容是否相同,盡量不使用== 或者!= 來(lái)比較,可以使用equal()來(lái)實(shí)現(xiàn)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
windows系統(tǒng)配置Java開(kāi)發(fā)環(huán)境變量
這篇文章主要介紹了windows系統(tǒng)配置Java開(kāi)發(fā)環(huán)境變量,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2021-12-12Spring?Boot對(duì)接Oracle數(shù)據(jù)庫(kù)具體流程
這篇文章主要給大家介紹了關(guān)于Spring?Boot對(duì)接Oracle數(shù)據(jù)庫(kù)的具體流程,本文將介紹如何在Spring Boot中連接Oracle數(shù)據(jù)庫(kù)的基本配置,包括添加依賴、配置數(shù)據(jù)源、配置JPA等,需要的朋友可以參考下2023-11-11Netty分布式pipeline傳播inbound事件源碼分析
這篇文章主要為大家介紹了Netty分布式pipeline傳播inbound事件的源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03JAVASE系統(tǒng)實(shí)現(xiàn)抽卡功能
這篇文章主要為大家詳細(xì)介紹了JAVASE系統(tǒng)實(shí)現(xiàn)抽卡功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11詳解配置spring-boot-actuator時(shí)候遇到的一些小問(wèn)題
這篇文章主要介紹了詳解配置spring-boot-actuator時(shí)候遇到的一些小問(wèn)題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Java使用Spring Batch處理大規(guī)模數(shù)據(jù)的實(shí)踐分享
在處理大規(guī)模數(shù)據(jù)的場(chǎng)景中,批處理是一個(gè)非常常見(jiàn)且必要的操作,Java中的Spring Batch是一個(gè)強(qiáng)大的框架,能夠幫助我們高效地執(zhí)行復(fù)雜的批處理任務(wù),本文將帶大家了解如何使用Spring Batch處理大規(guī)模數(shù)據(jù),并通過(guò)代碼示例展示如何實(shí)現(xiàn)高效的批處理,需要的朋友可以參考下2024-10-10