Home > Java > javaTutorial > What does void mean in java?

What does void mean in java?

下次还敢
Release: 2024-05-01 18:18:48
Original
353 people have browsed it

void in Java means that the method does not return any value and is used for method declaration (no return type is used), method invocation (no need to receive a return value), null pointer representation, wildcard type and lambda expression parameters.

What does void mean in java?

The meaning of void in Java

void is a keyword in Java, mainly used in the following scenarios :

1. Method declaration

When a method does not return any value, you can use the void keyword in the method declaration instead of the return type. For example:

<code class="java">public void printMessage() {
    System.out.println("Hello, world!");
}</code>
Copy after login

2. Method call

When calling a method with a return type of void, there is no need to use a variable to receive its return value. For example:

<code class="java">printMessage(); // 输出 "Hello, world!"</code>
Copy after login

3. Other uses

void can also be used for:

  • Represents a null pointer (i.e. null)
  • Declare a wildcard type that does not use generics
  • Indicates that there are no parameters in the lambda expression

Notes:

  • If a method is declared void but actually returns a non-void value, the compiler will report an error.
  • Cannot assign a value to a variable of type void or perform any operation on it.

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!

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