最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Error using ternary operator and method overloading of wrapper class in Java 8 only - Stack Overflow

programmeradmin1浏览0评论

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.

  1. type of ternary operator must be int, not Integer
  2. 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.

  1. type of ternary operator must be int, not Integer
  2. 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
  • Given that it doen’t happen in later versions, I would write this off as a bug that's been fixed. Probably, the compiler maintains a choice of int or Integer for the ternary, and can’t choose between append(int) and append(Object). If your problem is that you need to compile it in Java 8, I expect that it works if you cast the ternary with either (int) or (Integer). – njlarsson Commented Mar 28 at 7:18
  • Why are you writing code like this? Even if the compiler figures out what it means, it leaves me and other readers in doubt. That’s bad. Insert a cast or explicit unboxing or boxing to make your intension crystal clear since no one knows the entire JLS by heart. – Anonymous Commented Mar 28 at 9:18
Add a comment  | 

1 Answer 1

Reset to default 6

This 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).

发布评论

评论列表(0)

  1. 暂无评论