java.lang.Void類的解析與使用詳解
今天在查看源碼的時(shí)候發(fā)現(xiàn)了 java.lang.Void 的類。這個(gè)有什么作用呢?
先通過(guò)源碼查看下
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() {}
}
從源碼中發(fā)現(xiàn)該類是final的,不可繼承,并且構(gòu)造是私有的,也不能 new。
那么該類有什么作用呢?
下面是我們先查看下 java.lang.Integer 類的源碼
我們都知道 int 的包裝類是 java.lang.Integer
從這可以看出 java.lang.Integer 是 int 的包裝類。
同理,通過(guò)如下 java.lang.Void 的源碼可以看出 java.lang.Void 是 void 關(guān)鍵字的包裝類。
public static final Class<Void> TYPE = (Class<Void>) Class.getPrimitiveClass("void");
Void 使用
Void類是一個(gè)不可實(shí)例化的占位符類,如果方法返回值是Void類型,那么該方法只能返回null類型。
示例如下:
public Void test() {
return null;
}
使用場(chǎng)景一:
Future<Void> f = pool.submit(new Callable() {
@Override
public Void call() throws Exception {
......
return null;
}
});
比如使用 Callable接口,該接口必須返回一個(gè)值,但實(shí)際執(zhí)行后沒(méi)有需要返回的數(shù)據(jù)。 這時(shí)可以使用Void類型作為返回類型。
使用場(chǎng)景二:
通過(guò)反射獲取所有返回值為void的方法。
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());
}
}
}
}
執(zhí)行結(jié)果:
main hello wait wait wait notify notifyAll
ps:下面介紹java.lang.Void 與 void的比較及使用
void關(guān)鍵字表示函數(shù)沒(méi)有返回結(jié)果,是java中的一個(gè)關(guān)鍵字。
java.lang.Void是一種類型。例如給Void引用賦值null。
Void nil = null;
通過(guò)Void類的代碼可以看到,Void類型不可以繼承與實(shí)例化。
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作為函數(shù)的返回結(jié)果表示函數(shù)返回null(除了null不能返回其它類型)。
Void function(int a, int b) {
//do something
return null;
}
在泛型出現(xiàn)之前,Void一般用于反射之中。例如,下面的代碼打印返回類型為void的方法名。
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());
}
}
}
}
泛型出現(xiàn)后,某些場(chǎng)景下會(huì)用到Void類型。例如Future<T>用來(lái)保存結(jié)果。Future的get方法會(huì)返回結(jié)果(類型為T)。
但如果操作并沒(méi)有返回值呢?這種情況下就可以用Future<Void>表示。當(dāng)調(diào)用get后結(jié)果計(jì)算完畢則返回后將會(huì)返回null。
另外Void也用于無(wú)值的Map中,例如Map<T,Void>這樣map將具Set<T>有一樣的功能。
因此當(dāng)你使用泛型時(shí)函數(shù)并不需要返回結(jié)果或某個(gè)對(duì)象不需要值時(shí)候這是可以使用java.lang.Void類型表示。
總結(jié)
以上所述是小編給大家介紹的java.lang.Void的類 解析與使用詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Spring MVC保證Controller并發(fā)安全的方法小結(jié)
在 Spring MVC 中,默認(rèn)情況下,@Controller 是單例的,這意味著所有請(qǐng)求共享一個(gè) Controller 實(shí)例,為確保并發(fā)安全,Spring 并不會(huì)自動(dòng)對(duì) Controller 進(jìn)行線程安全保護(hù),本文給大家介紹了Spring MVC保證Controller并發(fā)安全的方法,需要的朋友可以參考下2024-11-11
Spring中Service注入多個(gè)實(shí)現(xiàn)類的方法詳解
這篇文章主要介紹了Spring中Service注入多個(gè)實(shí)現(xiàn)類的方法詳解,Spring是一個(gè)開(kāi)源的Java框架,用于構(gòu)建企業(yè)級(jí)應(yīng)用程序,它提供了許多功能,如依賴注入、面向切面編程、數(shù)據(jù)訪問(wèn)、Web開(kāi)發(fā)等,需要的朋友可以參考下2023-07-07
Java異步判斷線程池所有任務(wù)是否執(zhí)行完成的操作方法
這篇文章主要介紹了Java異步判斷線程池所有任務(wù)是否執(zhí)行完成的方法,在這個(gè)示例中,我使用了傳統(tǒng)的匿名內(nèi)部類來(lái)創(chuàng)建Callable任務(wù)(同時(shí)也提供了Lambda表達(dá)式的注釋),以便與各種Java版本兼容,需要的朋友可以參考下2024-07-07

