In Java, a method can return a value to the caller. The return type defines what kind of value a method sends back after execution. Return types make methods reusable, predictable, and type-safe.
void means the method returns nothingreturn statement ends method executionThe return type is written before the method name. The returned value must match the declared type.
// Method returning an integer value
int add(int a, int b) {
return a + b;
}
// Method with void return type
void greet() {
System.out.println("Hello, Java");
}
// Method returning a String object
String getName() {
return "Debashis";
}
The add() method returns an integer result, which can be stored in a variable. The greet() method performs an action but returns nothing. The getName() method returns a String object.
Select a method type to see how data moves!
void only when no value is neededreturnbooleanvoid method into one that returns a value