Home > Java > javaTutorial > Detailed explanation of void method in Java

Detailed explanation of void method in Java

清浅
Release: 2019-03-14 10:22:11
Original
8551 people have browsed it

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

Detailed explanation of void method in Java

[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.

The role of void

(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; 
}
Copy after login

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;
Copy after login

The compiler will report an error, indicating that the type cannot be converted, so in order to pass the compilation, we add a mandatory type Convert p1 = (float*)p2, so that the compilation can be successful.

The void is different at this time, it can accept other types of things


void *p1; 
int *p2; 
p1 =p2;
Copy after login

But this does not mean that p2 = p1 can also be used, so the empty type also has a certain inclusiveness, and The type is not inclusive

(2) Limitation on function return value

The function return value can also be void, as shown in the following case

void* memcpy(void* desc, const void* src,size_t len)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template