今天有網友在群里面發了兩道讓他“感覺要命”的筆試題,大意如下
題目一
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);
}
}
請問結果是:
A.編譯出錯 B.運行出錯 C.輸出“String” D.輸出 "Object"
這道題目可以說是老掉牙了,博主最初在某Java面試寶典上看到過的這道題目,后面在Java解惑中也看到過這道題目。顯然這涉及了重載方法調用的選擇的問題。JLS中關于方法重載的選擇是這么說的:
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.第一階段,不允許裝拆箱轉換,或者變長參數,如果找不到可用的方法則執行第二階段
2.的二階段,執行允許裝拆箱轉換的重載解析,但是不允許變長參數,如果找不到可用的方法則執行第三階段
3.第三階段,允許重載與變長參數方法,裝拆箱相結合
4.如果在任意一個階段標識出了若干可運用的方法,則選擇最具體的一個。
顯然 null既可以轉化為 Object 也可以轉化為 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);
}
}
這時,三個函數在第一階段都可以匹配,String與Integer都比Object要更加精確匹配,但是String與Integer無法區分那個更加精確,編譯器就無法判斷調用那哪個方法了,因此編譯出錯。
題目二
public class CompoundAssignment{
public static void main(String[] args) {
int x = 10;
x += x -= x -= x;
System.out.println("x="+x);
}
}
輸出結果為?
咋一看,結合順序從右到左,所以應該是 x+= ( x-= ( x-=x ) ) 代入數值應該是 0 += (0 -= (10 -= 10)) 結果應該為 0
好吧,我們運行一下看看
結果:
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”,大意是“復合賦值左端的值是在右端計算之前保存的”,所以結果應該是 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);
}
}
您看結果是多少呢?
圉于博主的水平,理解可能有所偏差,還望各位大佬不吝指正!
參考:The Java Language Specification, Java SE 8 Edition