总结
特点 | |
void | 用于无返回值的方法定义 |
Void |
|
剖析
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() {} ---不能实例化
}
样例
package com.bat.example;
public class TestVoid {
public static void main(String[] args) {
test1();
Void v1 = test2();
System.out.println(v1);
TestDao<Void> testDao = new TestDao<>();
Void v2 = testDao.get();
System.out.println(v2);
}
public static void test1(){
}
public static Void test2(){
return null;
}
static class TestDao<T> {
public T get(){
return null;
}
}
}