Java數(shù)據(jù)類型Integer與int的區(qū)別詳細(xì)解析
Integer與int的區(qū)別
如果面試官問Integer與int的區(qū)別:估計(jì)大多數(shù)人只會(huì)說道兩點(diǎn),Ingeter是int的包裝類,int的初值為0,Ingeter的初值為null。
但是如果面試官再問一下Integer i = 1;int ii = 1; i==ii為true還是為false?
估計(jì)就有一部分人答不出來了,如果再問一下其他的,估計(jì)更多的人會(huì)頭腦一片混亂。
所以我對它們進(jìn)行了總結(jié),希望對大家有幫助。
package com.test; /** * * @author 劉玲 * */ public class TestInteger { /** * @param args */ public static void main(String[] args) { int i = 128; Integer i2 = 128; Integer i3 = new Integer(128); //Integer會(huì)自動(dòng)拆箱為int,所以為true System.out.println(i == i2); System.out.println(i == i3); System.out.println("**************"); Integer i5 = 127;//java在編譯的時(shí)候,被翻譯成-> Integer i5 = Integer.valueOf(127); Integer i6 = 127; System.out.println(i5 == i6);//true /*Integer i5 = 128; Integer i6 = 128; System.out.println(i5 == i6);//false */ Integer ii5 = new Integer(127); System.out.println(i5 == ii5); //false Integer i7 = new Integer(128); Integer i8 = new Integer(123); System.out.println(i7 == i8); //false } }
首先,17行和18行輸出結(jié)果都為true,因?yàn)镮nteger和int比都會(huì)自動(dòng)拆箱(jdk1.5以上)。
22行的結(jié)果為true,而25行則為false,很多人都不動(dòng)為什么。
其實(shí)java在編譯Integer i5 = 127的時(shí)候,被翻譯成-> Integer i5 = Integer.valueOf(127);
所以關(guān)鍵就是看valueOf()函數(shù)了。
只要看看valueOf()函數(shù)的源碼就會(huì)明白了。JDK源碼的valueOf函數(shù)式這樣的:
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); }
看一下源碼大家都會(huì)明白,對于-128到127之間的數(shù),會(huì)進(jìn)行緩存,Integer i5 = 127時(shí),會(huì)將127進(jìn)行緩存,下次再寫Integer i6 = 127時(shí),就會(huì)直接從緩存中取,就不會(huì)new了。
所以22行的結(jié)果為true,而25行為false。
對于27行和30行,因?yàn)閷ο蟛灰粯樱詾閒alse。
我對于以上的情況總結(jié)如下:
①無論如何,Integer與new Integer不會(huì)相等。不會(huì)經(jīng)歷拆箱過程,i3的引用指向堆,而i4指向?qū)iT存放他的內(nèi)存(常量池),他們的內(nèi)存地址不一樣,所以為false
②兩個(gè)都是非new出來的Integer,如果數(shù)在-128到127之間,則是true,否則為false。java在編譯Integer i2 = 128的時(shí)候,被翻譯成-> Integer i2 = Integer.valueOf(128);而valueOf()函數(shù)會(huì)對-128到127之間的數(shù)進(jìn)行緩存
③兩個(gè)都是new出來的,都為false
④int和integer(無論new否)比,都為true,因?yàn)闀?huì)把Integer自動(dòng)拆箱為int再去比。
到此這篇關(guān)于Java數(shù)據(jù)類型Integer與int的區(qū)別詳細(xì)解析的文章就介紹到這了,更多相關(guān)Integer與int的區(qū)別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring實(shí)戰(zhàn)之ResourceLoaderAware加載資源用法示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之ResourceLoaderAware加載資源用法,結(jié)合實(shí)例形式分析了spring使用ResourceLoaderAware加載資源相關(guān)配置與操作技巧,需要的朋友可以參考下2020-01-01IntelliJ IDEA 2018 最新激活碼(截止到2018年1月30日)
這篇文章主要介紹了IntelliJ IDEA 2018 最新激活碼(截止到2018年1月30日)的相關(guān)資料,需要的朋友可以參考下2018-01-01JPA在不寫sql的情況下如何實(shí)現(xiàn)模糊查詢
文章介紹了在項(xiàng)目中實(shí)現(xiàn)模糊查詢的幾種方法,包括使用JPA的API、JPQL、QueryByExample和@Query注解,通過實(shí)現(xiàn)Specification接口和定義接口繼承JpaRepository,可以方便地進(jìn)行單字段和多字段的模糊查詢,文章還提到了BINARY函數(shù)的使用以及查詢結(jié)果的返回2024-11-11Spring Boot 自動(dòng)裝配原理及 Starter 實(shí)現(xiàn)原理解析
SpringBoot通過@SpringBootApplication注解簡化了依賴引入和配置,該注解包括@SpringBootConfiguration、@EnableAutoConfiguration和@ComponentScan三部分,感興趣的朋友跟隨小編一起看看吧2024-09-09Java中MyBatis的動(dòng)態(tài)語句詳解
這篇文章主要介紹了Java中MyBatis的動(dòng)態(tài)語句詳解,動(dòng)態(tài) SQL 是 MyBatis 的強(qiáng)大特性之一,通過不同參數(shù)生成不同的 SQL,可以動(dòng)態(tài)地對數(shù)據(jù)持久層進(jìn)行操作,而不需要每個(gè)數(shù)據(jù)訪問操作都要進(jìn)行手動(dòng)地拼接 SQL 語句,需要的朋友可以參考下2023-08-08