what is void in java
In Java, void is a return type keyword that means “this method does not
return any value.”
Quick Scoop: What Is void in Java?
When you write a method in Java, you must tell the compiler what type of value
that method will give back (like int, String, double), or explicitly say
that it returns nothing using void.
Example:
java
public class Main {
public static void printGreeting() { // return type is void
System.out.println("Hello, World!");
}
public static void main(String[] args) {
printGreeting(); // we just call it, no value is returned
}
}
Here:
public static→ modifiers.void→ tells Java this method returns no value.printGreeting→ method name.- Method body just does something (prints text) and then finishes.
Why Use void?
You use void when your method’s job is to perform an action rather than
compute and return a result.
Common cases:
- Printing to the console (
System.out.println(...)).
- Updating object fields or global state.
- Writing to a file, sending a network request, logging, etc.
Because the method returns nothing, you do not write return someValue;
inside it (you can use plain return; if you just want to exit early).
void vs Non-void (Quick Contrast)
java
// Void method: does something, returns nothing
public void increaseCount() {
count++; // modifies state
}
// Non-void: calculates and returns a value
public int getIncreasedCount() {
return count + 1; // returns int
}
- Use
voidwhen the caller doesn’t need a result back, only the side effect. - Use a type (
int,String, etc.) when you want to compute and hand back a value.
Extra: void vs Void (the Class)
Besides the void keyword, Java also has a Void class used in some
advanced, generic or reflection scenarios (e.g., Callable<Void> to indicate
“this logically returns nothing, so it always returns null”).
You usually won’t touch Void in beginner code, but it exists so that “no
value” can be represented where a class type is required.
TL;DR: In Java,
voidis used in method declarations to say “this method does not return any value; it just performs an action and then finishes.”
Information gathered from public forums or data available on the internet and portrayed here.