今天有網(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);
}
}
請問結(jié)果是:
A.編譯出錯 B.運行出錯 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:
- 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.
- 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. - 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
翻譯過來就是:
1.第一階段胧砰,不允許裝拆箱轉(zhuǎn)換鳍鸵,或者變長參數(shù),如果找不到可用的方法則執(zhí)行第二階段
2.的二階段尉间,執(zhí)行允許裝拆箱轉(zhuǎn)換的重載解析偿乖,但是不允許變長參數(shù),如果找不到可用的方法則執(zhí)行第三階段
3.第三階段哲嘲,允許重載與變長參數(shù)方法贪薪,裝拆箱相結(jié)合
4.如果在任意一個階段標識出了若干可運用的方法,則選擇最具體的一個眠副。
顯然 null既可以轉(zhuǎn)化為 Object 也可以轉(zhuǎn)化為 String類型画切,確切地說“The null reference can always be assigned or cast to any reference type ”。但是String類型比Object更加具體囱怕,所以匹配String類型的方法霍弹。即選擇 C
當然如果要是匹配的多個方法中一樣的精確,那么則會在編譯時報錯
如:
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ù)在第一階段都可以匹配典格,String與Integer都比Object要更加精確匹配,但是String與Integer無法區(qū)分那個更加精確台丛,編譯器就無法判斷調(diào)用那哪個方法了耍缴,因此編譯出錯。
題目二
public class CompoundAssignment{
public static void main(String[] args) {
int x = 10;
x += x -= x -= x;
System.out.println("x="+x);
}
}
輸出結(jié)果為挽霉?
咋一看防嗡,結(jié)合順序從右到左,所以應該是 x+= ( x-= ( x-=x ) ) 代入數(shù)值應該是 0 += (0 -= (10 -= 10)) 結(jié)果應該為 0
好吧侠坎,我們運行一下看看
結(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”闷煤,大意是“復合賦值左端的值是在右端計算之前保存的”,所以結(jié)果應該是 10 += (10 -= (10 -= 10)) 即 20
此外為了加深對這句話的理解,在此獻上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