Return Types in Java
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 → in...

Source: DEV Community
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 "Hell