java基礎(chǔ)面試題-new Integer, Integer.valueOf區(qū)別

面試中,有時(shí)候會(huì)問(wèn)到類似new Integer(100) Integer.valueOf(100)的區(qū)別?
==一般來(lái)說(shuō),對(duì)象都是在堆里創(chuàng)建的,所以所有對(duì)象的地址都不同,但真的是這樣的嗎?==

這個(gè)問(wèn)題,其實(shí)反應(yīng)了你對(duì)java包裝類型的源碼了解多少,有沒(méi)有真正的看過(guò)包裝類型的源碼。
要理解這個(gè)問(wèn)題,我們首先要知道java的基本數(shù)據(jù)類型有哪些?

java基本數(shù)據(jù)類型

基本類型 包裝類型 占用空間
boolean Boolean 1字節(jié)
byte Byte 2字節(jié)
char Character 2字節(jié)
short Short 2字節(jié)
int Integer 4字節(jié)
float Float 4字節(jié)
long Long 8字節(jié)
double Double 8字節(jié)

boolean 只有兩個(gè)值:true、false,可以使用 1 bit 來(lái)存儲(chǔ),但是具體大小沒(méi)有明確規(guī)定。JVM 會(huì)在編譯時(shí)期將 boolean 類型的數(shù)據(jù)轉(zhuǎn)換為 int,使用 1 來(lái)表示 true,0 表示 false。JVM 支持 boolean 數(shù)組,但是是通過(guò)讀寫 byte 數(shù)組來(lái)實(shí)現(xiàn)的。

字符串的數(shù)據(jù)解析成對(duì)應(yīng)的數(shù)據(jù)類型

   int x =Integer.parseInt("9");
   double c = Double.parseDouble("5");
   int b = Integer.parseInt("444",16);
   可具體參考包裝類型的api

基本類型轉(zhuǎn)化成string,可以直接+"",也可以轉(zhuǎn)換成包裝類型,使用包裝類型的api來(lái)轉(zhuǎn)換成string類型。

String test = 1 + "";
Integer value = Integer.valueOf("123");
String str = value.toString();
可具體參考包裝類型的api

拆箱裝箱

基本類型都有對(duì)應(yīng)的包裝類型,基本類型與其對(duì)應(yīng)的包裝類型之間的賦值使用自動(dòng)裝箱與拆箱完成。

Integer x = 10;     // 裝箱
int y = x;         // 拆箱

緩沖池的使用

==java針對(duì)一定數(shù)據(jù)范圍的數(shù)據(jù),使用valueOf的方法,會(huì)重復(fù)使用緩沖池里的數(shù)據(jù),不會(huì)返回新的對(duì)象,既然某個(gè)數(shù)據(jù)范圍使用非常多,為什么要頻繁創(chuàng)建和銷毀對(duì)象呢,這是jvm層的一個(gè)自動(dòng)優(yōu)化。==

new Integer(100) 與 Integer.valueOf(100) 的區(qū)別在于:

  • new Integer(100) 每次都會(huì)新建一個(gè)對(duì)象;
  • Integer.valueOf(100) 會(huì)使用緩存池中的對(duì)象,多次調(diào)用會(huì)取得同一個(gè)對(duì)象的引用。
Integer x = new Integer(100);
Integer y = new Integer(100);
System.out.println(x == y);    // false
Integer z = Integer.valueOf(100);
Integer k = Integer.valueOf(100);
System.out.println(z == k);   // true

valueOf() 方法的實(shí)現(xiàn)比較簡(jiǎn)單,就是先判斷值是否在緩存池中,如果在的話就直接返回緩存池的內(nèi)>容。

public static Integer valueOf(int i) {
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}

在 Java 8 中,Integer 緩存池的大小默認(rèn)為 -128~127。緩沖池的下界是 - 128,不可改變,可以通過(guò)屬性"java.lang.Integer.IntegerCache.high"設(shè)置緩存池的最大值。

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;
}

緩沖池默認(rèn)范圍

包裝類型 緩沖池
Boolean true 或false
Byte 所有字節(jié)
Short -128-127
Integer -128-127
Character \u0000 to \u007F
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容