[英]Does null have Object type?
Why does this output: "inside String argument method"? 为什么这个输出:“内部String参数方法”? Isn't null "Object" type?
不是null“对象”类型?
class A {
void printVal(String obj) {
System.out.println("inside String argument method");
}
void printVal(Object obj) {
System.out.println("inside Object argument method");
}
public static void main(String[] args) {
A a = new A();
a.printVal(null);
}
}
The most specific matching method will be called. 将调用最具体的匹配方法。 More information here:
更多信息:
是的,但null也是String类型和所有其他类型,因此它选择要调用的最具体方法。
More specifically, a null
literal will call the String version, for the reasons outlined by the other answers. 更具体地说,由于其他答案概述的原因,
null
文字将调用String版本。
On the other hand: 另一方面:
class A {
void printVal(String obj) {
System.out.println("inside String argument method");
}
void printVal(Object obj) {
System.out.println("inside Object argument method");
}
public static void main(String[] args) {
A a = new A();
Object myObj = null;
a.printVal(myObj);
}
}
will print "inside Object argument method." 将打印“内部对象参数方法”。 As will settings the type for
myObj
to any type other than String
. 将
myObj
的类型设置为除String
之外的任何类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.