The void method is a non-instantiable placeholder in Java. When the method is decorated with void, it means there is no return value. It has two functions: limiting function parameters and limiting function return values. Anyone who has studied Java knows that void means empty in Java, and it is a keyword in Java. , the following article will introduce you to the relevant knowledge of this value in detail, which has a certain reference effect. I hope it will be helpful to everyone
[Recommended courses :Java Tutorial】
void method introduction
at There are only two data types in Java, namely basic data types and reference data types, and void is a non-instantiable placeholder class in Java. When a method is defined as void, it means there is no return value.
(1) Restrictions on function parameters
If the function has no parameters, then it should will declare its parameters as void. As shown below
int function(void) { return 1; }
When passing a parameter to it, an error will be reported, indicating that this function does not accept any parameters.
If the function parameter can be a pointer of any type, the reason why it should be declared as void is as follows:float *p1; int *p2; p1 = p2;
void *p1; int *p2; p1 =p2;
The function return value can also be void, as shown in the following case
void* memcpy(void* desc, const void* src,size_t len)
In this way, any type of pointer can be passed into memcpy, which also reflects the meaning of the memory operation function, because the object manipulated by the memory operation function is a memory area, and no matter what type this memory area is, and this function The return value is also void
Summary: The above is the entire content of this article. I hope it will be helpful for everyone to learn void in Java.
The above is the detailed content of Detailed explanation of void method in Java. For more information, please follow other related articles on the PHP Chinese website!