java - On Void 返回类型

标签 java void

如果需要返回一个 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)。事实上,Voidfinal 且不可实例化,这意味着 null 是函数返回 Void 的唯一对象可以返回。

当然 public void blah() {...}(小写 v)不必返回任何内容。

如果您想了解 Void 的可能用途,请参阅 Uses for the Java Void Reference Type?

关于java - On Void 返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8806761/

相关文章:

c - 为什么没有参数的函数(与实际函数定义相比)可以编译?

c - 如何根据条件对 void 指针进行类型转换?

java - Findbugs 错误 - Bug : br is null guaranteed to be dereferenced on exception path

java - 未知单词的 IDF 分数?

java - 拍照时 Activity 被破坏

java - for循环中的public void方法;无法接收循环变量

java - 需要额外过滤时重用流 - Java

java - 当执行 TestNG 套件时,正在使用旧版本的测试类

PostgreSQL 函数返回 void

java - 为什么不能使用 Void 作为 main 方法的返回类型