今天有網(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.編譯出錯(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:
- 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.如果在任意一個(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無法區(qū)分那個(gè)更加精確,編譯器就無法判斷調(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
此外為了加深對這句話的理解,在此獻(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