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灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP