如果需要返回一个 Void
类型,Javadoc 将其描述为
A class that is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void.
为什么下面仍然需要返回null
?
public Void blah() {
return null; // It seems to always want null
}
最佳答案
Void
是一个与任何其他类一样的类,因此返回 Void
的函数必须返回一个引用(例如 null
)。事实上,Void
是 final
且不可实例化,这意味着 null
是函数返回 Void
的唯一对象可以返回。
当然 public void blah() {...}
(小写 v
)不必返回任何内容。
如果您想了解 Void
的可能用途,请参阅 Uses for the Java Void Reference Type?
关于java - On Void 返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8806761/