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
java.lang.Void vs void vs Null
提问by Josh
What exactly is the difference between Void
, void
, and can I just use null
instead?
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 null
to Void
, because (1) Void
has no instances and thus there is no Void
object, and (2) casting null
to anything is rather useless because null is a valid value for any Object data type.
此外,没有必要强制转换null
为Void
,因为 (1)Void
没有实例,因此没有Void
对象,并且 (2) 强制转换null
为任何内容都毫无用处,因为 null 是任何对象数据类型的有效值。
Code should probably just be:
代码应该是:
myAsyncTask.execute(null);
回答by Javier
The most common use of Void
is for reflection, but that is not the only place where it may be used.
最常见的用途Void
是反射,但这不是它唯一可以使用的地方。
void
is a keyword that means that a function does not result a value.
void
是一个关键字,表示函数不会产生值。
java.lang.Void
is 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 Void
has 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.get
will 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 delete
operation returns a Future<Void>
. When get()
is invoked on that future, null
is 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 Map
without 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
Void
is "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)
void
is a return type signifying no return.
void
是表示不返回的返回类型。
null
is the absence of value.
null
是价值的缺失。
回答by Wug
java.lang.Void
is the boxed representation of the void
type. 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.Void
是void
类型的盒装表示。由于您不能拥有 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()
null
indicates no values. Used for objects as comparison to void which is used for methods.
null
表示没有值。用于对象作为与用于方法的 void 的比较。