Java

Chapter 30: Return Type in Java — void, Primitives, References, and Early return

Learn Java return types: void vs values, return statement rules, unreachable code, returning references, and early-return style for readable methods.

Author: Sushil Kumar

Java return typeJava void vs returnJava return statementJava early returnJava method return value

Chapter 30: Return Type in Java

This chapter is only about what a method sends back to its caller: the return type in the declaration and the return keyword in the body. Parameters were Chapter 29; overloading is Chapter 31.


1. Topic title

Return types: void, int, String, objects—and when execution stops at return


2. What it means

The declaration starts with the return type (or void):

static int doubleIt(int x) {
    return x * 2;
}
 
static void shout(String msg) {
    System.out.println(msg.toUpperCase());
    return; // optional for void
}
  • void — no value goes back to the caller. You may write bare return; to exit early.
  • Non-void — every code path that finishes normally must return a value compatible with the declared type (or throw). The compiler checks this.

3. Why it is used

Return values are how small methods compose: int total = add(a, b) + add(c, d); only works because add returns something you can feed to +.

They also keep side effects and answers separate: a method can compute without printing, letting the caller decide what to do with the result.


4. Real-world example

Tax helper: static BigDecimal computeVat(BigDecimal net, BigDecimal rate) returns money owed; the invoice service prints, logs, or persists—that is not the tax method’s job.


5. Java code example

public class ReturnTypeDemo {
 
    static int max(int a, int b) {
        if (a >= b) {
            return a;
        }
        return b;
    }
 
    static String label(int score) {
        if (score >= 40) {
            return "pass";
        }
        return "fail";
    }
 
    static int sign(int n) {
        if (n < 0) {
            return -1;
        }
        if (n == 0) {
            return 0;
        }
        return 1;
    }
 
    static void report(String name) {
        if (name == null || name.isBlank()) {
            System.out.println("(no name)");
            return;
        }
        System.out.println("Hello, " + name);
    }
 
    public static void main(String[] args) {
        System.out.println(max(3, 7));
        System.out.println(label(55));
        System.out.println(sign(-9));
        report("  ");
        report("Ana");
    }
}

6. Explanation of code

  • max: two branches each return an int; compiler knows all paths return.
  • label: returns String literals; type matches.
  • sign: three exits; still exhaustive for int input.
  • report: void; early return skips the greeting when name is useless.

7. Common mistakes

Forgetting return on one branch in a non-void method — compile error “missing return statement.”

Returning the wrong typereturn "42"; in an int method fails.

Code after return in the same block — unreachable; compile error.

Returning null for String when callers do not expect it—document or use Optional later.


8. Best practices

Prefer early returns in void and value methods to avoid deep nesting—readers see failures first.

Keep return expressions simple; use a local variable if the expression spans three lines.

Name methods after what they return when they are pure: computeTotal not doTotal.


9. Small practice task

Write static double average(int a, int b) that returns (a + b) / 2.0 (watch the .0). Call from main and print. Then add static boolean isEven(int n) returning n % 2 == 0 without an if-else—use a single return expression.


Beginner tip

Read the method signature as a promise: “I take these parameters and I give you this type back.” Break that promise and the compiler complains—that is a feature.