What is a Return Type?
A return type is the data type of the value that a method sends back to the calling method.
Syntax
returnType methodName(parameters) {
// code
return value;
}
- returnType → int, double, String, boolean, etc.
- return value; → sends result back
Example 1: Returning an Integer
public class Example1 {
public static void main(String[] args) {
Example1 obj = new Example1();
int result = obj.add(10, 5);
System.out.println("Result = " + result);
}
int add(int a, int b) {
return a + b;
}
}
Output
Here:
- Return type = int
- Method returns an integer value
Flow of execution
- main() method starts execution
- Object obj is created
- add(10, 5) method is called
- Inside method → a + b is calculated
- Value 15 is returned to main()
- Stored in result and printed
Example 2: Returning a String
public class Example2 {
public static void main(String[] args) {
Example2 obj = new Example2();
String message = obj.greet("Harini");
System.out.println(message);
}
String greet(String name) {
return "Hello " + name;
}
}
Output
Flow of execution
- main() starts
- Object is created
- greet("Harini") is called
- Method creates "Hello Harini"
- Returns string to main()
- Printed on screen
Example 3: Returning a Boolean
public class Example3 {
public static void main(String[] args) {
Example3 obj = new Example3();
boolean result = obj.isEven(4);
System.out.println("Is Even? " + result);
}
boolean isEven(int number) {
return number % 2 == 0;
}
}
Output
Flow of execution
- main() starts
- Object is created
- isEven(4) is called
- Condition 4 % 2 == 0 checked
- Returns true
- Printed as result
Example 4: void Return Type
public class Example4 {
public static void main(String[] args) {
Example4 obj = new Example4();
obj.display(); // calling method
}
void display() {
System.out.println("Hello");
}
}
Output
Flow of execution
- main() starts
- Object is created
- display() is called
- Method directly prints "Hello"
- No value returned




Top comments (0)