java 板


LINE

※ 引述《bennylu (減肥)》之銘言: : How about this example : method(5); : .. : void method(int... i) {System.out.print("int...");} : void method(long... i) {System.out.print("long...");} : 如果沒會錯意, 那3個步驟只能告訴我哪些method是符合資格的, : 但並沒有提到當有多個候選人時該如何作選擇, 所以也不會提到ambiguous問題, : 下面這篇文章和我有一樣的問題 : http://java.itags.org/java-certification/337/ 結論先說在前頭, 我的看法跟你連結裡 jesperyoung 所回覆的相同, 這確實是一個 compiler 的 bug。 === 在 JLS 15.12 Method Invocation Expressions 中有詳細的定義: http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12 在 15.12.2 提到: If several applicable methods have been identified during one of the three phases of applicability testing, then the most specific one is chosen. applicable method 的定義則是: A method is applicable if it is either applicable by subtyping (§15.12.2.2), applicable by method invocation conversion (§15.12.2.3), or it is an applicable variable arity method (§15.12.2.4). (剛好對應三個階段。) 註: JLS 對這三個階段的定義比較詳細, 我先前貼的直接將 generics method 忽略掉了。 完整定義請參考 15.12.2.2 ~ 15.12.2.4 在做 method resolution 時,會依序測試那三個階段, 如果在任何一個階段中, 只有一個 the most specific method,就可以選定它。 如果有多個 the most specific method,便是 ambiguous。 若一個都沒有,則繼續下一個階段。 剩下最後一個問題是,如何找出 most specific method? 當有兩個 method m1, m2 皆為 applicable 時, 並且 m1 的每一個 parameter 都是 m2 parameter 的 subtype 時, 我們稱 m1 is more specific than m2。 註: 上面為了方便說明,我忽略了 generics 和 varargs, 正確定義請參考 15.12.2.5 讓我直接舉兩個例子: void foo(int a, long b){} // m1 void foo(long a, int b){} // m2 當我呼叫 foo 並傳入兩個 int 時,如 foo(0,1), m1, m2 之間並不存在 more specific 關係, 因此 m1, m2 都是 most specific method,造成 ambiguous。 第二個例子: void foo(float a, long b){} // m1 void foo(long a, int b){} // m2 在這個例子中,因為 m2 的兩個參數都可以 implicitly casting 成 m1 的參數, 因此 m2 is more specific method than m1, 所以 resolution 的結果會是選擇 m2。 註: 對於 primitive type 來說,subtype 的定義為: "被箭頭指向的 type" 是 "指向它人的 type" 的 subtype, 即 int 是 byte, short, char 的 subtype。 詳細可參考 4.10.1。 8-bits 16-bits 32-bits 64-bits ----------------------------------------------- byte ──→ short ─┬→ int ──→ long (signed) │ ┌────┘ char ─┘ ↓ (unsigned) float ─→ double 結論就是,根據 JLS 的定義, void method(int... i) {System.out.print("int...");} void method(long... i) {System.out.print("long...");} method(5) 應該會執行參數為 int... 的 method, 但 compiler 卻沒有這樣做,並且認為這是 ambiguous。 而因為這個 bug 的 priority 是 Vary Low, 所以拖了近六年至今還沒被處理掉....XD --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.115.134.17 ※ 編輯: tkcn 來自: 59.115.134.17 (09/08 22:20)
1F:→ ogamenewbie:把 int 換成 Integer 也一樣嗎? 09/08 22:31
2F:→ ogamenewbie:歐, 沒事, 我以為原po還是 Integer... vs long... 09/08 22:47
3F:→ tkcn:如果是最後一個例子,換成 Integer 也一樣不正確 09/08 22:49
4F:→ ogamenewbie:不正確的意思是 compiler bug 還是 compiler error? 09/08 22:51
5F:→ tkcn:compiler 顯示 error,但定義是不會才對,所以也是 bug 09/08 22:55
6F:→ ogamenewbie:可是照定義不就是會走到 pass 3 然後 amb 掉嗎? 09/08 23:17
7F:→ ogamenewbie:對不起, 我是 int -> Integer, ... 依舊保留的意思 09/08 23:18
8F:→ tkcn:看不是很懂 @@ 09/08 23:21
9F:→ tkcn:好像看懂了,到phase 3時Integer還是比long more specific, 09/08 23:38
10F:→ tkcn:所以根據定義,會選Interger那method去執行 (但目前不是) 09/08 23:43
11F:→ ogamenewbie:請問一下在 JLS 的 pass 中是哪邊有關於 Integer... 09/09 01:02
12F:→ ogamenewbie:要 boxing 且 long 要作型轉才會在 pass 3 的情況下. 09/09 01:06
13F:→ ogamenewbie:boxing 會比型轉 more specific 的說明阿? @_@ 09/09 01:07
前面推文的時後完全忘記我可以回來修文章 Orz 我要說的範圍都在 15.12.2.5 之內, Integer 比 long more specific 這點應該毫無疑問(非 varargs 情況下), 這個部份在 phase 2 就會完成,根據 15.12.2.3: For 1<=i<=n, the type of ei, Ai, can be converted by method invocation conversion (§5.3) to Si. 節錄 5.3: Method invocation contexts allow the use of one of the following: a boxing conversion (§5.1.7) optionally followed by widening reference conversion an unboxing conversion (§5.1.8) optionally followed by a widening primitive conversion. 而根據 phase 3 定義,並沒有任何違反上述規則的條件。 ※ 編輯: tkcn 來自: 140.122.183.199 (09/09 09:20)
14F:推 ogamenewbie:不要漏掉...啊. (雖然我自己一直在漏掉 orz 09/09 11:52
15F:→ tkcn:我說 phase 3 就是指加上 varargs 了,定義沒衝突 09/09 11:54







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Gossiping站內搜尋

TOP