为什么`System.out.println(null);`give"方法println(char [])对于类型PrintStream错误“不明确”? [英] Why does `System.out.println(null);` give "The method println(char[]) is ambiguous for the type PrintStream error"?

查看:321
本文介绍了为什么`System.out.println(null);`give"方法println(char [])对于类型PrintStream错误“不明确”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用代码:

System.out.println(null);

显示错误:

The method println(char[]) is ambiguous for the type PrintStream

为什么 null 表示对象

推荐答案

接受引用类型的 PrintStream 中有3个 println 方法 - println(char x []) println(String x) println(Object x )

There are 3 println methods in PrintStream that accept a reference type - println(char x[]), println(String x), println(Object x).

当您传递 null 时,所有3都适用。方法重载规则更喜欢具有最特定参数类型的方法,因此未选择 println(Object x)

When you pass null, all 3 are applicable. The method overloading rules prefer the method with the most specific argument types, so println(Object x) is not chosen.

然后编译器无法在前两个之间进行选择 - println(char x [])& println(String x) - 因为 String 并不比 char [] ,反之亦然。

Then the compiler can't choose between the first two - println(char x[]) & println(String x) - since String is not more specific than char[] and vice versa.

如果要选择特定方法,请将null转换为所需类型。

If you want a specific method to be chosen, cast the null to the required type.

例如:

System.out.println((String)null);

这篇关于为什么`System.out.println(null);`give"方法println(char [])对于类型PrintStream错误“不明确”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆