The void keyword in Java programming language
In this tutorial, we will discuss The void keyword in java programming language
The void keyword is a Java keyword. This keyword allows us to create methods which do not return a value. Which means void is a special type of keyword in Java as a void keyword does not return value unlike int, double, float etc at method declaration.
Declaration
Syntax of the void keyword
return _type method_name(){
//statement(s)
}
Example
Example of a void method
void add(){
//statement(s)
}
Above method is a void method as it does not return any value to the main method.
Example void method within the class
public class Example _void{ // this is a class in java
public static void main(String args[]){
}
public void add(){ // this is a void method. It does not produce return value;
}
}
Program 1
public class voidEx{
public static void main (String args[]){
my_age(50);
}
static void my_age(int age){
System.out.println("My age is :"+age);
//No return
}
}
When the above code is executed, it produces the following results
The age is :50
In the above example, The method my_age is a void method passed as a parameter age(int) which does not return any value
There are other Java language keywords that are similar to this keyword
Suggested for you