A method can do more than perform an action. It can also calculate something, retrieve information, process input, and send the result back to the code that called it. This result is called a return value.
Return values are one of the most useful features of Java methods because they allow methods to behave like reusable processing units. A caller provides input, the method performs its task, and the resulting value can then be stored, displayed, compared, or passed to another method.
What Is a Return Value?
A return value is the value sent from a method back to the code that invoked it. A method that returns a value must declare the appropriate return type in its declaration.
static int add(int a, int b) {
return a + b;
}
The return type is int, and the statement return a + b; sends the calculated integer back to the caller.
The return Statement
Java uses the return statement to send a value back from a method.
static int getNumber() {
return 100;
}
When this method is invoked, the value 100 is returned to the calling code.
int number = getNumber();
System.out.println(number);
The returned value is assigned to the number variable.
Return Type Must Match the Returned Value
The declared return type determines what kind of value a method is expected to return.
static int getAge() {
return 25;
}
This is valid because the method declares int and returns an integer.
A method returning a double can return a decimal value:
static double getPrice() {
return 499.99;
}
Likewise, a method can return a String:
static String getMessage() {
return "Welcome to Java";
}
| Return Type | Example Return Value |
|---|---|
| int | return 100; |
| double | return 99.50; |
| boolean | return true; |
| char | return 'A'; |
| String | return "Java"; |
| Object | return student; |
Using a Returned Value
A returned value can be stored in a variable.
static int multiply(int a, int b) {
return a * b;
}
int result = multiply(6, 7);
System.out.println(result);
The method returns 42, which is stored in result.
The returned value can also be used directly without creating a separate variable.
System.out.println(multiply(6, 7));
This is useful when the result is needed only once.
Returning a Calculated Result
A common use of return values is to hide calculation details inside a method.
static double calculateArea(double length, double width) {
return length * width;
}
double area = calculateArea(12.5, 8.0);
The calling code does not need to know how the calculation is performed. It simply requests the area and receives the result.
Remember: A method should return a value when the caller needs the result of the operation.
Returning Boolean Values
Methods that answer yes-or-no questions commonly return a boolean.
static boolean isAdult(int age) {
return age >= 18;
}
The method returns either true or false.
boolean result = isAdult(21);
if (result) {
System.out.println("Adult");
}
This style is common in professional applications because a method can encapsulate a business rule while the caller simply uses its result.
Returning Strings
A method can return text when the caller needs a generated message or other textual information.
static String getGreeting(String name) {
return "Welcome, " + name;
}
String message = getGreeting("Anita");
System.out.println(message);
The method creates the message and returns it. The caller decides what to do with that message.
Returning Values from Conditional Statements
A method can return different values depending on a condition.
static String getResult(int marks) {
if (marks >= 40) {
return "Pass";
}
return "Fail";
}
When the marks are at least 40, the method returns "Pass". Otherwise, it returns "Fail".
Notice that once a return statement executes, the method immediately ends.
Return Statement Ends Method Execution
The return statement does two things: it provides a value to the caller and immediately terminates the current method execution.
static int checkNumber(int number) {
if (number > 0) {
return 1;
}
return -1;
}
Once return 1; executes, Java does not continue to the final return statement.
Returning from void Methods
A void method does not return a value. However, it can use return; by itself to stop execution early.
static void printPositive(int number) {
if (number <= 0) {
return;
}
System.out.println("Positive number: " + number);
}
If the number is zero or negative, the method ends immediately. No value is returned.
Important: return; in a void method exits the method, while return value; sends a value back to the caller.
Returning Objects
Java methods can return objects as well as primitive values.
class Student {
String name;
}
static Student createStudent() {
Student student = new Student();
student.name = "Rahul";
return student;
}
The method returns a reference to the newly created Student object.
Student student = createStudent();
System.out.println(student.name);
Returning objects is extremely common in real Java applications, especially when methods create or retrieve domain objects.
Returning Arrays
A method can also return an array.
static int[] getMarks() {
return new int[] {85, 90, 78, 92};
}
int[] marks = getMarks();
The caller receives the array and can then process its elements.
Returning the Result of Another Method
A method can directly return the result produced by another method.
static int square(int number) {
return number * number;
}
static int calculate(int number) {
return square(number) + 10;
}
This allows methods to be composed into larger operations without unnecessary temporary variables.
Methods Without Return Values
Not every method needs to return data. If its purpose is simply to perform an action, void is appropriate.
static void displayWelcome() {
System.out.println("Welcome to the application");
}
The method performs an action but does not produce a result for the caller.
| Method Type | Example | Purpose |
|---|---|---|
| void method | void printMessage() | Performs an action without returning a value. |
| Value-returning method | int calculateTotal() | Produces a result for the caller. |
| Boolean method | boolean isValid() | Answers a true-or-false question. |
| Object-returning method | Student getStudent() | Returns an object or object reference. |
Common Mistake: Missing Return Statement
If a method declares a return type other than void, Java expects a suitable value to be returned whenever execution reaches the end of the method.
static int getNumber() {
// Compilation error
}
The method promises to return an int but does not provide one.
Common Mistake: Wrong Return Type
static int getName() {
return "Rahul";
}
This is invalid because the method promises an int but attempts to return a String.
Common Mistake: Ignoring a Useful Return Value
A returned value can be ignored syntactically in many situations, but doing so may be poor design if the result is important.
static int calculateTotal(int price, int quantity) {
return price * quantity;
}
calculateTotal(100, 5);
The method calculates a useful result, but the caller does nothing with it. In production code, this should be intentional rather than accidental.
Best Practices
- Choose a return type that accurately describes the result.
- Use meaningful return values instead of printing from methods when the caller needs to make further decisions.
- Keep a method focused on producing one clear result or performing one clear action.
- Use boolean return values for clear yes-or-no questions.
- Avoid returning unrelated or ambiguous values that make the method difficult to understand.
Interview Insight
A common interview question is: “What is the purpose of the return statement?” A strong answer is: “The return statement terminates the current method and, when used with a value, sends that value back to the caller. The returned value must be compatible with the method's declared return type.”
Interviewers may also ask why a method uses void. The answer is that void indicates that the method does not provide a value to its caller.
Quick Revision
| Concept | Key Point |
|---|---|
| Return value | The result sent from a method back to its caller. |
| return | Ends method execution and can send a value to the caller. |
| Return type | Specifies the type of value a method can return. |
| void | Indicates that a method does not return a value. |
| Object return | A method can return an object or object reference. |
| Multiple returns | A method can contain different return statements in different execution paths. |
| Return compatibility | The returned value must be compatible with the declared return type. |
Return values allow Java methods to communicate results back to their callers, making methods useful not just for performing actions but also for calculations, validation, data retrieval, and business logic. Once return values are clear, the next important skill is learning exactly how Java executes a method when it is called: method invocation.
