java. Comparison between lang. void and void and introduction to their usage

Void is not a function, but a modifier of a method. Void means that the method does not return a value, which means that the method will only run the statements in the method, but will not return anything.

java. Lang.void is a type. For example, assign null to a void reference.

As you can see from the code of the void class, the void type cannot be inherited or instantiated.

Void as the return result of the function means that the function returns null (no other types can be returned except null).

Before generics, void was generally used in reflection. For example, the following code prints the method name whose return type is void.

After generics appear, the void type will be used in some scenarios. For example, future < T > is used to save the results. The get method of future will return a 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, null will be returned. In addition, void is also used in valueless maps, such as map < T, void > so that the map will have the same function as set < T >.

Therefore, when you use generics, the function does not need to return a result or an object does not need a value, you can use Java Lang. void type representation.

reference resources:

java. lava. Comparison between void and void,

http://stackoverflow.com/questions/10839042/what-is-the-difference-between-java-lang-void-and-void

How to judge whether a function returns void,

http://stackoverflow.com/questions/1924253/how-to-determine-by-reflection-if-a-method-returns-void

@H_ 301_ 55 @ summary

The above is about Java The comparison between lang. void and void and the introduction to how to use them. If you want to know more about Java Lang. void. Here is a java.lang.void class source code analysis, which is very appropriate. I hope it will be helpful to you. Interested friends can refer to: detailed explanation of the use method of Java atomicinteger class, discussion on Java recycling object marking and object secondary marking process, etc. if you have any questions, you can leave a message at any time for everyone to exchange and discuss.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>