java.lang.Void类的解析与使用详解

2025-05-29 0 64

今天在查看源码的时候发现了 java.lang.Void 的。这个有什么作用呢?

先通过源码查看下

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23
package java.lang;

/**

* The {@code Void} class is an uninstantiable placeholder class to hold a

* reference to the {@code Class} object representing the Java keyword

* void.

*

* @author unascribed

* @since JDK1.1

*/

public final

class Void {

/**

* The {@code Class} object representing the pseudo-type corresponding to

* the keyword {@code void}.

*/

@SuppressWarnings("unchecked")

public static final Class<Void> TYPE = (Class<Void>) Class.getPrimitiveClass("void");

/*

* The Void class cannot be instantiated.

*/

private Void() {}

}

从源码中发现该是final的,不可继承,并且构造是私有的,也不能 new。

那么该有什么作用呢?

下面是我们先查看下 java.lang.Integer 的源码

我们都知道 int 的包装是 java.lang.Integer

java.lang.Void类的解析与使用详解

从这可以看出 java.lang.Integer 是 int 的包装

同理,通过如下 java.lang.Void 的源码可以看出 java.lang.Void 是 void 关键字的包装

?

1
public static final Class<Void> TYPE = (Class<Void>) Class.getPrimitiveClass("void");

Void 使用

Void是一个不可实例化的占位符,如果方法返回值是Void型,那么该方法只能返回null型。

示例如下:

?

1

2

3
public Void test() {

return null;

}

使用场景一:

?

1

2

3

4

5

6

7

8
Future<Void> f = pool.submit(new Callable() {

@Override

public Void call() throws Exception {

......

return null;

}

});

比如使用 Callable接口,该接口必须返回一个值,但实际执行后没有需要返回的数据。 这时可以使用Void型作为返回型。

使用场景二:

通过反射获取所有返回值为void的方法。

?

1

2

3

4

5

6

7

8

9

10
public class Test {

public void hello() { }

public static void main(String args[]) {

for (Method method : Test.class.getMethods()) {

if (method.getReturnType().equals(Void.TYPE)) {

System.out.println(method.getName());

}

}

}

}

执行结果:

?

1

2

3

4

5

6

7
main

hello

wait

wait

wait

notify

notifyAll

ps:下面介绍java.lang.Void 与 void的比较及使用

void关键字表示函数没有返回结果,是java中的一个关键字。

java.lang.Void是一种型。例如给Void引用赋值null。

?

1
Void nil = null;

通过Void的代码可以看到,Void型不可以继承与实例化。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
public final

class Void {

/**

* The {@code Class} object representing the pseudo-type corresponding to

* the keyword {@code void}.

*/

@SuppressWarnings("unchecked")

public static final Class<Void> TYPE = (Class<Void>) Class.getPrimitiveClass("void");

/*

* The Void class cannot be instantiated.

*/

private Void() {}

}

Void作为函数的返回结果表示函数返回null(除了null不能返回其它型)。

?

1

2

3

4
Void function(int a, int b) {

//do something

return null;

}

在泛型出现之前,Void一般用于反射之中。例如,下面的代码打印返回型为void的方法名。

?

1

2

3

4

5

6

7

8

9

10
public class Test {

public void print(String v) {}

public static void main(String args[]){

for(Method method : Test.class.getMethods()) {

if(method.getReturnType().equals(Void.TYPE)) {

System.out.println(method.getName());

}

}

}

}

泛型出现后,某些场景下会用到Void型。例如Future<T>用来保存结果。Future的get方法会返回结果(型为T)。

但如果操作并没有返回值呢?这种情况下就可以用Future<Void>表示。当调用get后结果计算完毕则返回后将会返回null。

另外Void也用于无值的Map中,例如Map<T,Void>这样map将具Set<T>有一样的功能。

因此当你使用泛型时函数并不需要返回结果或某个对象不需要值时候这是可以使用java.lang.Void型表示。

总结

以上所述是小编给大家介绍的java.lang.Void的 解析与使用详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!

原文链接:http://www.jianshu.com/p/678292d341f8?utm_source=tuicool&utm_medium=referral

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java.lang.Void类的解析与使用详解 https://www.kuaiidc.com/113402.html

相关文章

发表评论
暂无评论