Java基礎之Integer使用的注意事項及面試題
JAVA中Integer對象的引用
JAVA中沒有指針一說,但也有引用的概念。這里要說的主要是Integer是不是同一個對象。
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);
}
結果:
b1=5000
c1=5000
從上面,首先這里要說明幾個,
1)、對于Integer來說,-128-127之間的整型已經(jīng)初始化放在IntegerCache中,如果是裝箱的話,就會從這里面取對象。
2)、b1=a1到底是數(shù)字賦值還是同一個對象?這個從結果實際就可以看出來,b1和a1指向同一個對象,而不是同一個數(shù)值
3)、c1=100,說明對于-128-127之間的數(shù)值,都是從IntegerCache中獲取的對象,100對應的Integer對象被改變后,后續(xù)對于100的裝箱都被改變。因為獲取cache中對象時用的是數(shù)組索引,而不是數(shù)值比較獲取的。
不過修改這個緩存會比較危險,不介意。誰知道什么jar包或者什么平臺來個100的裝箱,但得到結果又不是100,到時就崩潰了。
2、通過上面描述,那么如果改成這樣又是什么答案
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);
}
這又是什么答案。對于new的操作,是不進行裝箱的,而是在堆中生成對象的。
理解了裝箱、緩存、引用就不難理解了??梢宰约涸囋嚒?/p>
先來點基礎的知識
基本類型和包裝類的對應 byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean
上述的八中基本數(shù)據(jù)類型的對應關系只有 int->Integer char->Character 兩個變化較大,其余都只是將首字母轉換為小寫。
再來了解一下JDK5的新特性:自動裝箱和拆箱
自動裝箱:把基本類型轉換為包裝類類型
自動拆箱:把包裝類類型轉換為基本類型
public class Demo_Integer {
public static void main(String[] args) {
//JDK1.5之前
int a = 100;
Integer a1 = new Integer(a); //將基本數(shù)據(jù)類型包裝成對象,裝箱
int b = a1.intValue(); //將對象轉換為基本數(shù)據(jù)類型,拆箱
//JDK1.5之后
int x = 100;
Integer x1 = x; //自動裝箱,把基本數(shù)據(jù)類型轉換為對象
int y = x1 + x; //自動拆箱,把對象轉換為基本數(shù)據(jù)類型
}
}
注意事項
public class Demo_Integer {
public static void main(String[] args) {
Integer a = null;
int b = a + 100; //自動拆箱底層將會調用a.intValue(),a為null,自然會拋出 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 是在堆內存開辟空間的,自然比較地址值(==)都為false.
由于Integer重寫了equals方法,所以equals輸出都為true.
你可能感覺太簡單了,沒有任何技術含量,因為上面的不是重點,看下面代碼
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 -----------
原因:
為什么當int大于127就是兩個對象,127這個數(shù)字是不是覺得很熟悉?
-128到127是byte的取值范圍,如果在這個取值范圍內,自動裝箱就不會創(chuàng)建新對象了,而從常量池中獲取
超過了byte的取值范圍就會在創(chuàng)建新對象
自動裝箱其底層會調用valueOf()方法,簡單源碼分析(JDK1.8):
public final class Integer extends Number implements Comparable<Integer> {
public static Integer valueOf(int i) {
//當 i >= -128 且 i <= 127 時,會直接將取緩沖區(qū)中的對象
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);//超過了byte取值范圍會在堆內存創(chuà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種基本類型的包裝類和對象池
java中基本類型的包裝類的大部分都實現(xiàn)了常量池技術,這些類是Byte,Short,Integer,Long,Character,Boolean,另外兩種浮點數(shù)類型的包裝類則沒有實現(xiàn)。另外Byte,Short,Integer,Long,Character這5種整型的包裝類也只是在對應值小于等于127時才可使用對象池,也即對象不負責創(chuàng)建和管理大于127的這些類的對象
擴展知識
在jvm規(guī)范中,每個類型都有自己的常量池。常量池是某類型所用常量的一個有序集合,包括直接常量(基本類型,String)和對其他類型、字段、方法的符號引用。之所以是符號引用而不是像c語言那樣,編譯時直接指定其他類型,是因為java是動態(tài)綁定的,只有在運行時根據(jù)某些規(guī)則才能確定具體依賴的類型實例,這正是java實現(xiàn)多態(tài)的基礎。
在JVM中,類從被加載到虛擬機內存中開始,到卸載出內存為止,它的整個生命周期包括:加載、驗證、準備、解析、初始化、使用和卸載7個階段。而解析階段即是虛擬機將常量池內的符號引用替換為直接引用的過程。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關文章
Springboot整合mybatisplus時,使用條件構造器排序報錯問題及解決
這篇文章主要介紹了Springboot整合mybatisplus時,使用條件構造器排序報錯問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04
SpringCloud?Feign集成AOP的常見問題與解決
在使用?Spring?Cloud?Feign?作為微服務通信的工具時,我們可能會遇到?AOP?不生效的問題,這篇文章將深入探討這一問題,給出幾種常見的場景,分析可能的原因,并提供解決方案,希望對大家有所幫助2023-10-10
使用Mybatis如何實現(xiàn)刪除多個數(shù)據(jù)
這篇文章主要介紹了使用Mybatis如何實現(xiàn)刪除多個數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03

