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.
(int)or(Integer).