java - Puzzling "reference to println is ambiguous" compilation error -
this question has answer here:
if compile , run following class (with java se 7, if matters),
class foo { public static void main(string[] args) { system.out.println(true ? null : 42); // system.out.println(null); } }
i following output
null
so far, good. however, if uncomment second statement in main
, compilation error:
foo.java:5: error: reference println ambiguous, both method println(char[]) in printstream , method println(string) in printstream match system.out.println(null); ^
why java compiler throw error if argument of system.out.println
null
, doesn't if argument true ? null : 42
?
the type of expression true ? null : 42
integer
therefore unambiguous system.println(object)
should called.
if call system.println(null)
there multiple candidate methods , compiler can't decide 1 take.
Comments
Post a Comment