Java Void 引用类型的用途?
有一个 Java Void
-- 大写 V-- 引用类型。 我见过的唯一使用情况是参数化 Callable s
final Callable<Void> callable = new Callable<Void>() {
public Void call() {
foobar();
return null;
}
};
Java Void
引用类型还有其他用途吗? 除了 null
之外,还可以为它分配任何其他值吗? 如果是的话,你有例子吗?
There is a Java Void
-- uppercase V-- reference type. The only situation I have ever seen it used is to parameterize Callable
s
final Callable<Void> callable = new Callable<Void>() {
public Void call() {
foobar();
return null;
}
};
Are there any other uses for the Java Void
reference type? Can it ever be assigned anything other than null
? If yes, do you have examples?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
Void
已成为您不感兴趣的泛型参数的约定。您没有理由应该使用任何其他不可实例化类型,例如System
。它也经常用于
Map
值(尽管Collections.newSetFromMap
使用Boolean
因为地图不必接受null
值)和java.security.PrivilegedAction
。Void
has become convention for a generic argument that you are not interested in. There is no reason why you should use any other non-instantiable type, such asSystem
.It is also often used in for example
Map
values (althoughCollections.newSetFromMap
usesBoolean
as maps don't have to acceptnull
values) andjava.security.PrivilegedAction
.您可以使用反射创建 Void 实例,但它们没有任何用处。 Void 是一种指示泛型方法不返回任何内容的方法。
打印类似的东西
You can create instance of Void using reflections, but they are not useful for anything. Void is a way to indicate a generic method returns nothing.
prints something like
Future
就像魅力一样。 :)Future<Void>
works like charm. :)鉴于没有公共构造函数,我会说它不能被分配除
null
之外的任何东西。 正如您的示例所示,我仅将其用作“我不需要使用此通用参数”的占位符。它也可以用于反射,从它的 Javadoc< /a> 说:
Given that there are no public constructors, I would say it can't be assigned anything other than
null
. I've only used it as a placeholder for "I don't need to use this generic parameter," as your example shows.It could also be used in reflection, from what its Javadoc says:
所有原始包装类(
Integer
、Byte
、Boolean
、Double
等)都包含对静态TYPE
字段中对应的原始类,例如:Void
最初被创建为放置对void
类型的引用的地方:但是,使用
Void.TYPE
并没有真正获得任何好处。 当您使用void.class
时,您会更清楚地知道您正在使用void
类型执行某些操作。顺便说一句,上次我尝试时,BeanShell 无法识别
void.class,所以你必须在那里使用
Void.TYPE
。All the primitive wrapper classes (
Integer
,Byte
,Boolean
,Double
, etc.) contain a reference to the corresponding primitive class in a staticTYPE
field, for example:Void
was initially created as somewhere to put a reference to thevoid
type:However, you don't really gain anything by using
Void.TYPE
. When you usevoid.class
it's much clearer that you're doing something with thevoid
type.As an aside, the last time I tried it, BeanShell didn't recognise
void.class
, so you have to useVoid.TYPE
there.当您使用访问者模式时,当您想要确保返回值时,使用 Void 而不是 Object 会更简洁will be null
示例
当您要实现访问者时,您可以显式地将 OUT 设置为 Void,以便您知道访问者将始终返回 null,而不是使用 Object
When you use the visitor pattern it can be cleaner to use Void instead of Object when you want to be sure that the return value will be null
Example
When you will implement your visitor you can explicitly set OUT to be Void so that you know your visitor will always return null, instead of using Object
在泛型之前,它是为反射 API 创建的,用于保存 Method.getReturnType() 返回的 void 方法的 TYPE,对应于其他基本类型类。
编辑:来自 Void 的 JavaDoc:“Void 类是一个不可实例化的占位符类,用于保存对表示 Java 关键字 void 的 Class 对象的引用”。 在使用泛型之前,我知道除了反射之外没有其他用处。
Before generics, it was created for the reflection API, to hold TYPE returned by Method.getReturnType() for a void method, corresponding to the other primitive type classes.
EDIT: From the JavaDoc of Void: "The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void". Prior to Generics, I am aware of no use other than reflection.
由于无法实例化 Void,因此可以使用 Apache commons Null 对象,因此
在第一行中,您有一个对象,因此
aNullObject != null
成立,而在第二行中没有引用,所以noObjectHere == null
成立回答发帖人原来的问题,这样做的用途是区分“无”和“无”,这是完全不同的东西。
PS:对空对象模式说不
As you can't instantiate Void, you can use Apache commons Null object, so
in the first line, you have an object, so
aNullObject != null
holds, while in the second line there is no reference, sonoObjectHere == null
holdsTo answer the poster's original question, the usage for this is to differentiate between "the nothing" and "nothing", which are completely different things.
PS: Say no to Null object pattern
Void
的创建是为了包装其原始void
类型。 每个基本类型都有其对应的引用类型。Void
用于实例化泛型类或使用泛型方法、您不感兴趣的泛型参数。这是一个示例...这里,如您所见,我不 感兴趣想要从服务器获取任何我要求创建新注册的内容,但是公共接口 AsyncCallback{ .... } 是一个泛型接口,因此我提供
Void
因为泛型不接受原始类型Void
was created to wrap its primitivevoid
type. Every primitive type has its corresponding Reference type.Void
is used to instantiate a generic class or use of a generic method, a generic argument which you are not interested in. Here is an example...Here, as you can see, I don't want anything from the server that I am asking to create a new registration ,but
public interface AsyncCallback<T> { .... }
is a generic interface, so i provideVoid
since generics don't accept primitive types当您不需要
Attachment
对象时,它也常用于异步 IO 完成回调。 在这种情况下,您可以为 IO 操作指定 null 并实现CompletionHandler
。It is also commonly used on Async-IO completion callbacks when you dont have the need for an
Attachment
object. In that case you specify null to the IO operation and implementCompletionHandler<Integer,Void>
.这可能是罕见的情况,但有一次,我在方面类中使用了
Void
。这是一个在具有 @Log 注释的方法之后运行的方面,如果方法返回类型不为 void,则记录返回的方法和一些信息。
It may be rare case but once, I used
Void
in aspect classes.This was an aspect which runs after methods that has an
@Log
annotation, and logs the method returned and some info if the method return type is not void.