admin 管理员组文章数量: 1086019
I am using Java 8 and facing some weird error.
The compiler gives the following error:
error: reference to [method] is ambiguous
Here's my code
StringBuilder sb = new StringBuilder();
Stack<Integer> st = new Stack<>();
st.push(123);
sb.append(st.size() > 0 ? st.pop() : 0);
After some searching, I found out that Java convert the ternary operator ? Integer:int
to int
. Also, overloading resolution prefers append(int i)
over append(Object o)
(unboxing).
So as I understand, the code should not cause any errors.
- type of ternary operator must be
int
, notInteger
- Even though the result is
Integer
,append(int i)
should be invoked due to overloading resolution
Hence, there's no reason to cause ambiguity
This is weird and Only occurs with Java 8 (Not 11, 17 - I've tried)
And an even weirder thing is that this code works
sb.append(st.size() <= 0 ? 0 : st.pop());
Can anyone explain why? Or let me know is there anything I missed
I've tried to find problems at how ternary operator and overloading resolution works, but as I investigated, there's no reason to cause error.
I am using Java 8 and facing some weird error.
The compiler gives the following error:
error: reference to [method] is ambiguous
Here's my code
StringBuilder sb = new StringBuilder();
Stack<Integer> st = new Stack<>();
st.push(123);
sb.append(st.size() > 0 ? st.pop() : 0);
After some searching, I found out that Java convert the ternary operator ? Integer:int
to int
. Also, overloading resolution prefers append(int i)
over append(Object o)
(unboxing).
So as I understand, the code should not cause any errors.
- type of ternary operator must be
int
, notInteger
- Even though the result is
Integer
,append(int i)
should be invoked due to overloading resolution
Hence, there's no reason to cause ambiguity
This is weird and Only occurs with Java 8 (Not 11, 17 - I've tried)
And an even weirder thing is that this code works
sb.append(st.size() <= 0 ? 0 : st.pop());
Can anyone explain why? Or let me know is there anything I missed
I've tried to find problems at how ternary operator and overloading resolution works, but as I investigated, there's no reason to cause error.
Share Improve this question edited Mar 28 at 7:39 Mark Rotteveel 110k229 gold badges156 silver badges224 bronze badges asked Mar 28 at 6:46 bloopbloop 434 bronze badges 2 |1 Answer
Reset to default 6This is just a compiler bug in Java 8. This is the relevant bug report: JDK-8064464. This was not an issue in Java 7, and is fixed in Java 9.
You are absolutely correct that the type of the ternary operator expression is int
. This is a numeric conditional expression as defined by the JLS. Therefore overload resolution should have found append(int)
during phase 1 (strict invocation), and does not continue onto phase 2 (loose invocation).
本文标签: Error using ternary operator and method overloading of wrapper class in Java 8 onlyStack Overflow
版权声明:本文标题:Error using ternary operator and method overloading of wrapper class in Java 8 only - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744052231a2525227.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
(int)
or(Integer)
. – njlarsson Commented Mar 28 at 7:18