In Java, void represents "empty", that is, "returns nothing". When the method is declared, it means that the method has no return value. Void corresponds to a Void class. The Void class is modified with final and is a placeholder class that cannot be instantiated.
#Before, when I was learning the data types of C, I found that there are four data types in C, and there is actually an empty type in it. There is a saying in it: "There is a A class function does not need to return a function value to the caller after being called. This kind of function can be defined as an "empty type". Its type specifier is void. My first reaction at the time was "Since void is a type in C Empty type, so what is void in java?"
void is a keyword in java, which means returning nothing. We often use it in the development process, If a method does not need to return a value, you can use the void keyword, which is also the void keyword in the main method.
public static void getName() { String name = "username"; System.out.println(name); } //main方法 public static void main(String[] args) { }
First of all, we know that Java is a strongly typed language. From the method declaration syntax, we can see that each method must have a return value. The return value type of the method needs to be determined. When defining a method When using void modification, nothing is returned. It can be seen that void should also be regarded as a data type;
And we know that the commonly used data types in Java include 8 basic data types, reference types, etc. .
So what type should the void modifier correspond to?
In fact, void corresponds to a Void class:
The Void class is modified with final, indicating that it cannot be extended. In addition, the constructor is private and cannot be instantiated. ;
The Void class is a non-instantiable placeholder class used to save a reference to a Class object that represents the Java keyword void.
The above is the detailed content of What does void mean in java?. For more information, please follow other related articles on the PHP Chinese website!