java.lang.Void vs void vs Null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15538219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:58:21  来源:igfitidea点击:

java.lang.Void vs void vs Null

javaandroidnullvoid

提问by Josh

What exactly is the difference between Void, void, and can I just use nullinstead?

Void, void, 和我可以直接使用之间的区别是什么null

I'm asking this is because I'm looking at sample Android code where they used Void but Eclipse errors on it (it says Void cannot be resolved to a variable).

我问这是因为我正在查看示例 Android 代码,其中他们使用了 Void 但上面有 Eclipse 错误(它说Void cannot be resolved to a variable)。

My code that breaks is

我打破的代码是

public class MyAsyncTask extends AsyncTask<Void, Void, Boolean>{
    ...
}

I use it like this

我像这样使用它

MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute((Void),null);//this is the line that breaks  "Void cannot be resolved to a variable"

采纳答案by jedyobidan

You have an extra comma in your code.

您的代码中有一个额外的逗号。

myAsyncTask.execute((Void),null);
                        //^extra comma right here

Also, there is no need to cast nullto Void, because (1) Voidhas no instances and thus there is no Voidobject, and (2) casting nullto anything is rather useless because null is a valid value for any Object data type.

此外,没有必要强制转换nullVoid,因为 (1)Void没有实例,因此没有Void对象,并且 (2) 强制转换null为任何内容都毫无用处,因为 null 是任何对象数据类型的有效值。

Code should probably just be:

代码应该是:

myAsyncTask.execute(null);

回答by Javier

The most common use of Voidis for reflection, but that is not the only place where it may be used.

最常见的用途Void是反射,但这不是它唯一可以使用的地方。

voidis a keyword that means that a function does not result a value.

void是一个关键字,表示函数不会产生值。

java.lang.Voidis a reference type, then the following is valid:

java.lang.Void是引用类型,则以下内容有效:

 Void nil = null;

(so far it is not interesting...)

(到目前为止,它并不有趣......)

As a result type (a function with a return value of type Void) it means that the function *always * return null(it cannot return anything other than null, because Voidhas no instances).

作为结果类型(具有 type 返回值Void的函数)这意味着该函数 * 总是 * 返回null(它不能返回除 之外的任何内容null,因为Void没有实例)。

 Void function(int a, int b) {
    //do something
    return null;
 }

Why would I like a function that alwaysreturns null?

为什么我想要一个总是返回 null的函数?

Before the invention of generics, I didn't have an use case for Void.

在泛型发明之前,我没有Void.

With generics, there are some interesting cases. For instance, a Future<T>is a holder for the result of an asynchronous operation performed by other thread. Future.getwill return the operation value (of type T), and will block until the computation is performed.

对于泛型,有一些有趣的情况。例如,aFuture<T>是其他线程执行的异步操作结果的持有者。Future.get将返回操作值(类型T),并会阻塞直到执行计算。

But... what if there is nothing to return? Simple: use a Future<Void>. For instance, in Google App Engine the Asyncronous Datastore Service deleteoperation returns a Future<Void>. When get()is invoked on that future, nullis returned afterthe deletion is complete. One could write a similar example with Callables.

但是……万一没有什么可以返回呢?简单:使用Future<Void>. 例如,在 Google App Engine 中,异步数据存储服务delete操作返回一个Future<Void>. 当get()在那个未来调用时,在删除完成null返回。可以用Callable写一个类似的例子。

Another use case is a Mapwithout values, i.e. a Map<T,Void>. Such a map behaves like a Set<T>, then it may be useful when there is no equivalent implementation of Set(for instance, there is no WeakHashSet, then one could use a WeakHashMap<T,Void>).

另一个用例是Map没有值的 a ,即 a Map<T,Void>。这样的映射的行为类似于 a Set<T>,那么当没有等效的实现时它可能很有用Set(例如,没有WeakHashSet,则可以使用 a WeakHashMap<T,Void>)。

回答by Daedalus

Voidis "an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void." (from http://docs.oracle.com/javase/6/docs/api/java/lang/Void.html)

Void是“一个不可实例化的占位符类,用于保存对表示 Java 关键字 void 的 Class 对象的引用。” (来自http://docs.oracle.com/javase/6/docs/api/java/lang/Void.html

voidis a return type signifying no return.

void是表示不返回的返回类型。

nullis the absence of value.

null是价值的缺失。

回答by Wug

java.lang.Voidis the boxed representation of the voidtype. Since you can't have an instance of type void, it's mostly there for completeness and the very rare instance where you need it for reflection.

java.lang.Voidvoid类型的盒装表示。由于您不能拥有 type 的实例void,因此它主要用于完整性和非常罕见的实例,您需要它进行反射。

回答by subodhkarwa

void: java keyword indicates method will not return anything. Used for methods

void: java 关键字表示方法不会返回任何内容。用于方法

Void: Wrapper around void. It extends Object class.

Void: 包裹空洞。它扩展了 Object 类。

Needed as Generic does not support primitive datatypes. (void considered primitive is a complex case but this is just to explain)

需要,因为 Generic 不支持原始数据类型。(void 被认为是一个复杂的情况,但这只是为了解释)

Should be used for reflection

应该用于反射

Constructor<Void> constructor = Void.class.getConstructor()

nullindicates no values. Used for objects as comparison to void which is used for methods.

null表示没有值。用于对象作为与用于方法的 void 的比较。