Today, when I was checking the source code, I discovered the java.lang.Void class. What is the function of this?
Check it out first via the source code
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 unaccounted * @since JDK1.1 */public finalclass 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() {}}It is found from the source code that this class is final, cannot be inherited, and the construct is private, and cannot be new.
So what is the function of this category?
Here is the source code of the java.lang.Integer class first
We all know that the wrapper class of int is java.lang.Integer
From this we can see that java.lang.Integer is the wrapper class of int.
Similarly, through the following source code of java.lang.Void, we can see that java.lang.Void is a wrapper class for the void keyword.
public static final Class<Void> TYPE = (Class<Void>) Class.getPrimitiveClass("void");Void usage
The Void class is a non-institable placeholder class. If the method returns a Void type, then the method can only return a null type.
Examples are as follows:
public Void test() { return null;}Use scenario 1:
Future<Void> f = pool.submit(new Callable() { @Override public Void call() throws Exception { ...... return null; } });For example, using the Callable interface, the interface must return a value, but there is no data to be returned after actual execution. At this time, you can use the Void type as the return type.
Use scenario 2:
Reflect all methods with return value 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()); } } }}Execution results:
mainhellowaiwaitwaitnotifynotifyAll
ps: The following introduces the comparison and use of java.lang.Void and void
The void keyword means that the function does not return the result and is a keyword in Java.
java.lang.Void is a type. For example, assign a value of null to a Void reference.
Void nil = null;
Through the code of the Void class, we can see that the Void type cannot be inherited or instantiated.
public finalclass 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 as the return result of a function indicates that the function returns null (except null cannot return other types).
Void function(int a, int b) { //do something return null; }Before generics appeared, Void was generally used in reflection. For example, the following code prints the method name of the return type 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()); } } }} After generics appear, Void type will be used in some scenarios. For example, Future<T> is used to save the results. Future's get method will return the result (type T).
But what if the operation does not return a value? In this case, it can be represented by Future<Void> . When the result is calculated after calling get, it will return null after returning.
In addition, Void is also used in valueless maps. For example, Map<T,Void> , so map will have the same function as Set<T> .
Therefore, when you use generics, the function does not need to return the result or an object does not need the value. This can be represented by the java.lang.Void type.
Summarize
The above is a detailed explanation of the class analysis and use of java.lang.Void introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!