OneBite.Dev - Coding blog in a bite size

declare a void function without return value in java

Code snippet on how to declare a void function without return value in java

public void sayHello(){
  System.out.println("Hello!");
}

This code declares a void function in Java without returning a value. Java is a strongly typed language, so the function starts with “public void”, which signifies the return type. The name of the function is “sayHello”. Within the curly brackets, we include the code that will be executed when the function is called. In this case, we are simply printing a string “Hello!” to the console. Finally, the function comes to an end with a closing curly bracket.

java