Java基礎(chǔ)之Integer使用的注意事項(xiàng)及面試題
JAVA中Integer對(duì)象的引用
JAVA中沒(méi)有指針一說(shuō),但也有引用的概念。這里要說(shuō)的主要是Integer是不是同一個(gè)對(duì)象。
1、先看一段代碼:
public static void main(String[] args){
Integer a1 = 100;
Integer b1 = a1;//另一種也可以b1=100
Field field = null;
try {
field = a1.getClass().getDeclaredField("value");
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
field.setAccessible(true);
try {
field.set(a1, 5000);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("b1="+b1);
Integer c1 = 100;
System.out.println("c1="+c1);
}
結(jié)果:
b1=5000
c1=5000
從上面,首先這里要說(shuō)明幾個(gè),
1)、對(duì)于Integer來(lái)說(shuō),-128-127之間的整型已經(jīng)初始化放在IntegerCache中,如果是裝箱的話,就會(huì)從這里面取對(duì)象。
2)、b1=a1到底是數(shù)字賦值還是同一個(gè)對(duì)象?這個(gè)從結(jié)果實(shí)際就可以看出來(lái),b1和a1指向同一個(gè)對(duì)象,而不是同一個(gè)數(shù)值
3)、c1=100,說(shuō)明對(duì)于-128-127之間的數(shù)值,都是從IntegerCache中獲取的對(duì)象,100對(duì)應(yīng)的Integer對(duì)象被改變后,后續(xù)對(duì)于100的裝箱都被改變。因?yàn)楂@取cache中對(duì)象時(shí)用的是數(shù)組索引,而不是數(shù)值比較獲取的。
不過(guò)修改這個(gè)緩存會(huì)比較危險(xiǎn),不介意。誰(shuí)知道什么jar包或者什么平臺(tái)來(lái)個(gè)100的裝箱,但得到結(jié)果又不是100,到時(shí)就崩潰了。
2、通過(guò)上面描述,那么如果改成這樣又是什么答案
public static void main(String[] args){
Integer a1 = 200;
Integer b1 = a1;
Field field = null;
try {
field = a1.getClass().getDeclaredField("value");
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
field.setAccessible(true);
try {
field.set(a1, 5000);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("b1="+b1);
Integer c1 = 200;
System.out.println("c1="+c1);
}
3、那么再改一下
public static void main(String[] args){
Integer a1 = new Integer(100);
Integer b1 = a1;
Field field = null;
try {
field = a1.getClass().getDeclaredField("value");
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
field.setAccessible(true);
try {
field.set(a1, 5000);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("b1="+b1);
Integer c1 = 100;
System.out.println("c1="+c1);
}
這又是什么答案。對(duì)于new的操作,是不進(jìn)行裝箱的,而是在堆中生成對(duì)象的。
理解了裝箱、緩存、引用就不難理解了??梢宰约涸囋嚒?/p>
先來(lái)點(diǎn)基礎(chǔ)的知識(shí)
基本類型和包裝類的對(duì)應(yīng) byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean
上述的八中基本數(shù)據(jù)類型的對(duì)應(yīng)關(guān)系只有 int->Integer char->Character 兩個(gè)變化較大,其余都只是將首字母轉(zhuǎn)換為小寫。
再來(lái)了解一下JDK5的新特性:自動(dòng)裝箱和拆箱
自動(dòng)裝箱:把基本類型轉(zhuǎn)換為包裝類類型
自動(dòng)拆箱:把包裝類類型轉(zhuǎn)換為基本類型
public class Demo_Integer {
public static void main(String[] args) {
//JDK1.5之前
int a = 100;
Integer a1 = new Integer(a); //將基本數(shù)據(jù)類型包裝成對(duì)象,裝箱
int b = a1.intValue(); //將對(duì)象轉(zhuǎn)換為基本數(shù)據(jù)類型,拆箱
//JDK1.5之后
int x = 100;
Integer x1 = x; //自動(dòng)裝箱,把基本數(shù)據(jù)類型轉(zhuǎn)換為對(duì)象
int y = x1 + x; //自動(dòng)拆箱,把對(duì)象轉(zhuǎn)換為基本數(shù)據(jù)類型
}
}
注意事項(xiàng)
public class Demo_Integer {
public static void main(String[] args) {
Integer a = null;
int b = a + 100; //自動(dòng)拆箱底層將會(huì)調(diào)用a.intValue(),a為null,自然會(huì)拋出 NullPointerException
System.out.println(b);
}
}
面試題
public class Demo_Integer {
public static void main(String[] args) {
Integer i1 = new Integer(97);
Integer i2 = new Integer(97);
System.out.println(i1 == i2);
System.out.println(i1.equals(i2));
System.out.println("-----------");
Integer i3 = new Integer(197);
Integer i4 = new Integer(197);
System.out.println(i3 == i4);
System.out.println(i3.equals(i4));
System.out.println("-----------");
}
}
Output: false true ----------- false true -----------
原因:
new 是在堆內(nèi)存開(kāi)辟空間的,自然比較地址值(==)都為false.
由于Integer重寫了equals方法,所以equals輸出都為true.
你可能感覺(jué)太簡(jiǎn)單了,沒(méi)有任何技術(shù)含量,因?yàn)樯厦娴牟皇侵攸c(diǎn),看下面代碼
public class Demo_Integer {
public static void main(String[] args) {
Integer i1 = 127;
Integer i2 = 127;
System.out.println(i1 == i2);
System.out.println(i1.equals(i2));
System.out.println("-----------");
Integer i3 = 128;
Integer i4 = 128;
System.out.println(i3 == i4);
System.out.println(i3.equals(i4));
System.out.println("-----------");
}
}
Output: true true ----------- false true -----------
原因:
為什么當(dāng)int大于127就是兩個(gè)對(duì)象,127這個(gè)數(shù)字是不是覺(jué)得很熟悉?
-128到127是byte的取值范圍,如果在這個(gè)取值范圍內(nèi),自動(dòng)裝箱就不會(huì)創(chuàng)建新對(duì)象了,而從常量池中獲取
超過(guò)了byte的取值范圍就會(huì)在創(chuàng)建新對(duì)象
自動(dòng)裝箱其底層會(huì)調(diào)用valueOf()方法,簡(jiǎn)單源碼分析(JDK1.8):
public final class Integer extends Number implements Comparable<Integer> {
public static Integer valueOf(int i) {
//當(dāng) i >= -128 且 i <= 127 時(shí),會(huì)直接將取緩沖區(qū)中的對(duì)象
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);//超過(guò)了byte取值范圍會(huì)在堆內(nèi)存創(chuàng)建
}
//內(nèi)部類充當(dāng)緩沖區(qū)
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() {}
}
}
8種基本類型的包裝類和對(duì)象池
java中基本類型的包裝類的大部分都實(shí)現(xiàn)了常量池技術(shù),這些類是Byte,Short,Integer,Long,Character,Boolean,另外兩種浮點(diǎn)數(shù)類型的包裝類則沒(méi)有實(shí)現(xiàn)。另外Byte,Short,Integer,Long,Character這5種整型的包裝類也只是在對(duì)應(yīng)值小于等于127時(shí)才可使用對(duì)象池,也即對(duì)象不負(fù)責(zé)創(chuàng)建和管理大于127的這些類的對(duì)象
擴(kuò)展知識(shí)
在jvm規(guī)范中,每個(gè)類型都有自己的常量池。常量池是某類型所用常量的一個(gè)有序集合,包括直接常量(基本類型,String)和對(duì)其他類型、字段、方法的符號(hào)引用。之所以是符號(hào)引用而不是像c語(yǔ)言那樣,編譯時(shí)直接指定其他類型,是因?yàn)閖ava是動(dòng)態(tài)綁定的,只有在運(yùn)行時(shí)根據(jù)某些規(guī)則才能確定具體依賴的類型實(shí)例,這正是java實(shí)現(xiàn)多態(tài)的基礎(chǔ)。
在JVM中,類從被加載到虛擬機(jī)內(nèi)存中開(kāi)始,到卸載出內(nèi)存為止,它的整個(gè)生命周期包括:加載、驗(yàn)證、準(zhǔn)備、解析、初始化、使用和卸載7個(gè)階段。而解析階段即是虛擬機(jī)將常量池內(nèi)的符號(hào)引用替換為直接引用的過(guò)程。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
如何修改FeginCilent定義的服務(wù)名到指定服務(wù)
這篇文章主要介紹了修改FeginCilent定義的服務(wù)名到指定服務(wù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Springboot整合mybatisplus時(shí),使用條件構(gòu)造器排序報(bào)錯(cuò)問(wèn)題及解決
這篇文章主要介紹了Springboot整合mybatisplus時(shí),使用條件構(gòu)造器排序報(bào)錯(cuò)問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
SpringBoot實(shí)現(xiàn)國(guó)際化的操作步驟
國(guó)際化(Internationalization) 是指為了適應(yīng)不同語(yǔ)言、文化和地區(qū)的用戶,使軟件能夠方便地進(jìn)行本地化修改的過(guò)程,本文介紹了SpringBoot 國(guó)際化功能的簡(jiǎn)單使用,感興趣的朋友可以參考下2024-02-02
Java Shutdown Hook場(chǎng)景使用及源碼分析
shutdown hook 就是一個(gè)簡(jiǎn)單的已初始化但是未啟動(dòng)的線程,本文詳細(xì)的介紹了Java Shutdown Hook場(chǎng)景使用及源碼分析,感興趣的朋友可以參考一下2021-06-06
servlet監(jiān)聽(tīng)器的學(xué)習(xí)使用(三)
這篇文章主要為大家詳細(xì)介紹了servlet監(jiān)聽(tīng)器學(xué)習(xí)使用的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
SpringCloud?Feign集成AOP的常見(jiàn)問(wèn)題與解決
在使用?Spring?Cloud?Feign?作為微服務(wù)通信的工具時(shí),我們可能會(huì)遇到?AOP?不生效的問(wèn)題,這篇文章將深入探討這一問(wèn)題,給出幾種常見(jiàn)的場(chǎng)景,分析可能的原因,并提供解決方案,希望對(duì)大家有所幫助2023-10-10
SpringBoot中調(diào)用通用URL的實(shí)現(xiàn)
在 Spring Boot 應(yīng)用程序中,有時(shí)候我們需要調(diào)用一些通用的 URL 接口,本文主要介紹了SpringBoot中調(diào)用通用URL的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
使用Mybatis如何實(shí)現(xiàn)刪除多個(gè)數(shù)據(jù)
這篇文章主要介紹了使用Mybatis如何實(shí)現(xiàn)刪除多個(gè)數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

