Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the usage and meaning of void in Java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the usage and meaning of void in Java". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the usage and meaning of void in Java".

For example:

So what is the underlying principle? Let's analyze it a little bit.

Let's first understand the basic meaning of this word, according to the results of the web query, as follows:

It shows that the meaning of the word void itself is blank and empty. In the Java language, the keyword void is also commonly used as the return value of a method, indicating that the return value is empty. You can use this keyword when defining a method that does not return a value, and if you need to return it in your code, you can use the return keyword, for example:

In fact, in Java, there is a class java.lang.Void that corresponds to the void keyword, as follows:

From the above source code, Void is actually the encapsulation type of void, which is generally referred to as a placeholder. This may seem a bit like the relationship between Integer and int, but this is not the case. Void is special: first, it cannot be instantiated; second, it has no built-in instances and no literal values that can be used.

So it usually doesn't make sense to declare a Void variable because there is no instance to assign. So the only thing that can assign a Void variable is null.

The only place where Void is available is to determine the type of reflection. For example:

But void.class can also be used here instead. So Void is actually the encapsulation of void.

So since void means there is no return value, why not just leave it unwritten? Do you need a void placeholder? Let's take a look at the underlying language of Java, that is, the CumberCure + language. There are generally two usage scenarios in void +: one is to limit the return of the function, and the other is to limit the parameters of the function. The second usage cannot be used in Java, so here we only talk about di.

Why do you need to limit the return of a function? in fact, in some compilers in the early C language, if you do not write the return value of the function, it may be treated by the compiler as returning an integer value (note, most compilers now generally report errors or warnings). But many programmers mistook it for void. For example:

Here, if the result of the program running is output: 2 + 3 = 5, it means that the function with no return value is the int function, not void.

In the C++ language, there is a very strict type safety check, which does not allow this to happen (meaning that functions do not add type declarations). However, the compiler may not think so, for example, the above add function compiled without error, without warning and running correctly in Visual compliant 6.0, so it cannot be expected that the compiler will do strict type checking.

Therefore, in order to avoid unnecessary problems (problems caused by non-standard programming), any function must be specified its type without omission when writing the Cpicard Category + program. If the function does not return a value, be sure to declare it as a void type. This is not only the need of good readability of the program, but also the requirement of programming standardization.

And the bottom layer of Java is the use of Cellular codes, and it also has good programming requirements, so there are keywords of void as placeholders.

Thank you for your reading. the above is the content of "what is the usage and meaning of void in Java". After the study of this article, I believe you have a deeper understanding of the usage and meaning of void in Java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report