Keyword in Java

The void keyword in Java programming language

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

The byte keyword in Java

The short keyword in Java

The int keyword in Java

The long keyword in Java

The double keyword in Java

The float keyword in Java

The boolean keyword in java

The char keyword in Java

 

Suggested for you

Keywords in Java language

Keywords in C++ language

Keywords in C language

Keywords in Python language

 

 

 

The protected keyword in Java programming language
The int keyword in Java programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Share
Published by
Karmehavannan

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

9 months ago