我正在使用代码:
System.out.println(null);
显示错误:
The method println(char[]) is ambiguous for the type PrintStream
为什么null
不代表Object
?
最佳答案
PrintStream
中有 3 个 println
方法接受引用类型 - println(char x[])
, println(字符串 x)
, println(Object x)
.
当你传递 null
时,所有 3 都适用。方法重载规则更喜欢具有最具体参数类型的方法,因此不选择 println(Object x)
。
那么编译器无法在前两者之间进行选择 - println(char x[])
& println(String x)
- 因为 String
并不比 char[]
更具体,反之亦然。
如果您想选择特定的方法,请将 null 转换为所需的类型。
例如:
System.out.println((String)null);
关于java - 为什么 `System.out.println(null);` 给出 "The method println(char[]) is ambiguous for the type PrintStream error"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36325277/