溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Java基礎(chǔ)知識(shí)精選 你答對(duì)了幾道?

發(fā)布時(shí)間:2020-10-24 21:54:33 來(lái)源:腳本之家 閱讀:163 作者:小柒2012 欄目:編程語(yǔ)言

沒(méi)有技術(shù)深度是大多程序員的一種常態(tài)。

但是當(dāng)你成為一個(gè)資深的工程師的時(shí)候,很多公司并不希望你還是那樣平庸,沒(méi)有深度。雖然你會(huì)納悶,我就算有深度你們也不一定用得上呀?然而到了這個(gè)級(jí)別的人需求量并不像初中級(jí)開(kāi)發(fā)那么多,公司更理性和穩(wěn)妥的做法是選擇有深度的人,不是嗎?

Integer比較

看下面這段有意思的代碼,對(duì)數(shù)字比較敏感的小伙伴有沒(méi)有發(fā)現(xiàn)異常?

public static void main(String[] args) {
    Integer a = 128,b=128; 
    Integer c = 127,d=127;
       
    System.out.println(a==b); 
    System.out.println(c==d);
}

如果你的回答是false,false,可能你有一定的基礎(chǔ),知道Integer是一個(gè)封裝類(lèi)。當(dāng)然如果你的答案是true,true的話(huà),也在一定的認(rèn)知范圍之內(nèi),但是基礎(chǔ)知識(shí)掌握的不夠好。

好了,我們運(yùn)行main方法,正確答案應(yīng)該是false,true。前幾年這道題出現(xiàn)在很多面試題中,當(dāng)然你也會(huì)說(shuō)了,我會(huì)做項(xiàng)目就ok了,用到查就是了,何必要知道,這我沒(méi)話(huà)說(shuō)。

其實(shí)當(dāng)我們給一個(gè)Integer對(duì)象賦一個(gè)int值的時(shí)候,會(huì)調(diào)用Integer類(lèi)的靜態(tài)方法valueOf,讓我們看下源代碼是怎么實(shí)現(xiàn)的。

IntegerCache方法有明確的注釋?zhuān)彺娣秶?,如何修改等等?/p>

 /**
   * Cache to support the object identity semantics of autoboxing for values between
   * -128 and 127 (inclusive) as required by JLS.
   *
   * The cache is initialized on first usage. The size of the cache
   * may be controlled by the -XX:AutoBoxCacheMax=<size> option.
   * During VM initialization, java.lang.Integer.IntegerCache.high property
   * may be set and saved in the private system properties in the
   * sun.misc.VM class.
   */

  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) {
        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);
      }
      high = h;

      cache = new Integer[(high - low) + 1];
      int j = low;
      for(int k = 0; k < cache.length; k++)
        cache[k] = new Integer(j++);
    }

    private IntegerCache() {}
  }
 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);
}

神奇不神奇,其實(shí)代碼描述的很清晰,如果整型字面量的值介于-128到127之間,就不會(huì)new一個(gè)新的Integer對(duì)象,而是直接引用常量池中的Integer對(duì)象,所以上面的運(yùn)行結(jié)果是a==b=false,而c==d=true。

String比較

接下來(lái)這道題,相對(duì)來(lái)說(shuō)應(yīng)該比較簡(jiǎn)單了。

public static void main(String[] args) {
    String s1 = "abc"; 
    String s2 = "abc"; 
    String s3 = new String("abc"); 
    System.out.println(s1 == s2); 
    System.out.println(s1 == s3);
  }

小伙伴們看了是不是很熟悉?可能有的人一眼就掃出了答案true,false。當(dāng)然沒(méi)有掃出正確答案的小伙伴們也不要?dú)怵H,下面跟大家分析分析為毛是這么一個(gè)答案。

按照==的語(yǔ)法來(lái)看, 首先s1、s2、s3是三個(gè)不同的對(duì)象,常理來(lái)說(shuō),輸出都會(huì)是false。然而程序的運(yùn)行結(jié)果確實(shí)true、false。第二個(gè)輸出false可以理解,第一個(gè)輸出true就又讓人費(fèi)解了。

我們知道一些基本類(lèi)型的變量和對(duì)象的引用變量都是在函數(shù)的棧內(nèi)存中分配,而堆內(nèi)存中則存放new 出來(lái)的對(duì)象和數(shù)組。然而除此之外還有一塊區(qū)域叫做常量池。

像我們通常想String s1 = "abc";這樣申明的字符串對(duì)象,其值就是存儲(chǔ)在常量池中。當(dāng)我們創(chuàng)建String s1 = "abc"這樣一個(gè)對(duì)象之后,"abc"就存儲(chǔ)到了常量池(也可叫做字符串池)中。

當(dāng)我們創(chuàng)建引用String s2 = "abc" 的時(shí)候,Java底層會(huì)優(yōu)先在常量池中查找是否存在"abc",如果存在則讓s2指向這個(gè)值,不會(huì)重新創(chuàng)建,如果常量池中沒(méi)有則創(chuàng)建并添加的池中。這就是為什么答案是true 和false的原因。

Integer與int比較

public static void main(String[] args) {
    Integer a = new Integer(128); 
    int b = 128; 
    Integer c = new Integer(6); 
    Integer d = new Integer(6); 
    System.out.println(a == b); 
    System.out.println(c == d); 
  }

相信又有不少小伙伴懵比了吧,ture還是false?還是直接公布答案吧,true,false。

c == d=false,我覺(jué)得沒(méi)什么好說(shuō)的,可能有的小伙伴要問(wèn)了不是-128-127被緩存起來(lái)了么?但是我們這里的Integer是new出來(lái)的,并不是用的緩存,所以結(jié)果是false。

a == b=true,大家注意一下這里的b是int類(lèi)型,當(dāng)int和Integer做==比較的時(shí)候,Integer類(lèi)型會(huì)自動(dòng)拆箱,也就是把Integer轉(zhuǎn)成int類(lèi)型,所以這里進(jìn)行比較的是int類(lèi)型的值,所以結(jié)果即為true。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI