有一个Java Void- 大写的V-- 引用类型.我见过它唯一的情况是参数化Callables
final Callable<Void> callable = new Callable<Void>() {
public Void call() {
foobar();
return null;
}
};
Run Code Online (Sandbox Code Playgroud)
Java Void引用类型还有其他用途吗?可以分配除了以外的任何东西null吗?如果是的话,你有例子吗?
由于在Java中使用泛型,我最终必须实现具有Void返回类型的函数:
public Void doSomething() {
//...
}
Run Code Online (Sandbox Code Playgroud)
并且编译器要求我返回一些东西.现在我回来了null,但我想知道这是不是很好的编码实践......
我也试过Void,void,Void.class,void,没有回报可言,但都不会在所有的工作.(出于或多或少的明显原因)(详情请参阅此答案)
Void.TYPE什么?new Void()堂课的一般用途是什么?编辑:只是为了免除你的支持:我问的是V oid,而不是v oid.类Void,而不是保留关键字Void.
首先,我不知道如何恰当地表达这个问题,所以这是一个建议。
假设我们有以下重载方法:
void execute(Callable<Void> callable) {
try {
callable.call();
} catch (Exception e) {
e.printStackTrace();
}
}
<T> T execute(Supplier<T> supplier) {
return supplier.get();
}
void execute(Runnable runnable) {
runnable.run();
}
Run Code Online (Sandbox Code Playgroud)
从这张表出发,我从另一个问题中得到了答案
Supplier () -> x
Consumer x -> ()
BiConsumer x, y -> ()
Callable () -> x throws ex
Runnable () -> ()
Function x -> y
BiFunction x,y -> z
Predicate x -> boolean
UnaryOperator x1 -> x2
BinaryOperator x1,x2 -> x3
Run Code Online (Sandbox Code Playgroud)
以下是我在本地得到的结果:
Supplier () -> …Run Code Online (Sandbox Code Playgroud)