由兩道筆試題想到的

今天有網(wǎng)友在群里面發(fā)了兩道讓他“感覺要命”的筆試題,大意如下
題目一

public class Interview{
    public static void fn(String str){System.out.println("String");}
    public static void fn(Object o){System.out.println("Object");}
    public static void main(String[] args) {
        fn(null);
    }
}
請(qǐng)問結(jié)果是:
A.編譯出錯(cuò) B.運(yùn)行出錯(cuò) C.輸出“String” D.輸出 "Object"

這道題目可以說是老掉牙了,博主最初在某Java面試寶典上看到過的這道題目,后面在Java解惑中也看到過這道題目。顯然這涉及了重載方法調(diào)用的選擇的問題。JLS中關(guān)于方法重載的選擇是這么說的:

The remainder of the process is split into three phases, to ensure compatibility with versions of the Java programming language prior to Java SE 5.0. The phases are:

  1. The first phase (§15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the second phase.
  2. The second phase (§15.12.2.3) performs overload resolution while allowing boxing and unboxing, but still precludes the use of variable arity method invocation. If no applicable method is found during this phase then processing
    continues to the third phase.
  3. The third phase (§15.12.2.4) allows overloading to be combined with variable arity methods, boxing, and unboxing.
    If several applicable methods have been identified during one of the three phases of applicability testing, then the most specific one is chosen, as specified in section

翻譯過來(lái)就是:

1.第一階段,不允許裝拆箱轉(zhuǎn)換,或者變長(zhǎng)參數(shù),如果找不到可用的方法則執(zhí)行第二階段
2.的二階段,執(zhí)行允許裝拆箱轉(zhuǎn)換的重載解析,但是不允許變長(zhǎng)參數(shù),如果找不到可用的方法則執(zhí)行第三階段
3.第三階段,允許重載與變長(zhǎng)參數(shù)方法,裝拆箱相結(jié)合
4.如果在任意一個(gè)階段標(biāo)識(shí)出了若干可運(yùn)用的方法,則選擇最具體的一個(gè)。
顯然 null既可以轉(zhuǎn)化為 Object 也可以轉(zhuǎn)化為 String類型,確切地說“The null reference can always be assigned or cast to any reference type ”。但是String類型比Object更加具體,所以匹配String類型的方法。即選擇 C
當(dāng)然如果要是匹配的多個(gè)方法中一樣的精確,那么則會(huì)在編譯時(shí)報(bào)錯(cuò)

如:

public class Interview{
    public static void fn(String str){
        System.out.println("String");
    }
    public static void fn(Object o){
        System.out.println("Object");
    }
    public static void fn(Integer i){
        System.out.println("Integer");
    }
    public static void main(String[] args) {
        fn(null);
    }
}

這時(shí),三個(gè)函數(shù)在第一階段都可以匹配,String與Integer都比Object要更加精確匹配,但是String與Integer無(wú)法區(qū)分那個(gè)更加精確,編譯器就無(wú)法判斷調(diào)用那哪個(gè)方法了,因此編譯出錯(cuò)。


題目二

public class CompoundAssignment{
    public static void main(String[] args) {
            int x = 10;
            x += x -= x -= x;
           System.out.println("x="+x);
    }
}
輸出結(jié)果為?

咋一看,結(jié)合順序從右到左,所以應(yīng)該是 x+= ( x-= ( x-=x ) ) 代入數(shù)值應(yīng)該是 0 += (0 -= (10 -= 10)) 結(jié)果應(yīng)該為 0
好吧,我們運(yùn)行一下看看
結(jié)果:

x=20

什么?怎么是20?-_- 好吧,咱們又得翻一下JLS了。
在JLS中的Example 15.26.2-2.有這么一句話“Value Of Left-Hand Side Of Compound Assignment Is Saved Before Evaluation Of Right-Hand Side”,大意是“復(fù)合賦值左端的值是在右端計(jì)算之前保存的”,所以結(jié)果應(yīng)該是 10 += (10 -= (10 -= 10)) 即 20
此外為了加深對(duì)這句話的理解,在此獻(xiàn)上JLS中的例子

class Test{
    public static void main(String[] args) {
        int k = 1;
        k += (k = 4) * (k + 2);
        System.out.println("k = "+k);   
    }
}

您看結(jié)果是多少呢?

圉于博主的水平,理解可能有所偏差,還望各位大佬不吝指正!
參考:The Java Language Specification, Java SE 8 Edition

最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,650評(píng)論 5 6
  • 木心老先生在《從前慢》里這樣說, 從前的日色變得慢,車,馬,郵件都慢,一生只夠愛一個(gè)人。 是啊,一生只夠愛一個(gè)人。...
    像你眉眼輕嘲閱讀 334評(píng)論 2 1
  • 奇葩上司什么都不會(huì),還要防著你不說,看你不順眼還要害你;這都算了,嘴巴還賤,到處散播你什么都不會(huì)的名聲,決意要把你...
    我與世界的的對(duì)抗閱讀 364評(píng)論 0 0
  • 親愛的,看著你又要發(fā)脾氣的樣子,我真擔(dān)心你。請(qǐng)你現(xiàn)在趕快做一次長(zhǎng)長(zhǎng)的足夠長(zhǎng)的深呼吸,對(duì),就是這樣子,閉起雙眼,鼓起...
    陳曉蓮閱讀 438評(píng)論 6 4
  • (小說) 誰(shuí)都有過美好的童年! 爬上高高的樹上掏鳥窩,淌入渾濁的河水逮小魚,瞞著家長(zhǎng)偷偷地去池塘洗澡,月色朦朧下,...
    燕山松閱讀 472評(píng)論 34 32

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