Skip to lesson

Return a value from a method

A method can send a result back to the code that called it. The return statement sends the value, and the return type says what kind of value it is.

Return type and return

printArea prints the area, but the shop also needs that number to work out the cost of the tiles. Write a return type in place of void, and return sends the answer back to the call.

int floor = area(12, 10); sends 12 and 10 into the method area. Inside, return length * width; works out 120 and sends it back. The call is replaced by 120, which is stored in floor.

Example 1 · Return the area, then use it

TileCost.javaJava
public class TileCost {    public static void main(String[] args) {        int floor = area(12, 10);        int cost = floor * 45;        System.out.println("Area: " + floor + " sq ft");        System.out.println("Tiles: Rs. " + cost);    }
    static int area(int length, int width) {        return length * width;    }}
Output
Area: 120 sq ft
Tiles: Rs. 5400

What this code does

  • static int area(…): int in place of void promises that every call gives back an int.
  • return length * width; sends 120 back. The call area(12, 10) takes that value, and it is stored in floor.
  • Now main can calculate with it: floor * 45 is the cost of tiles at Rs. 45 a square foot.

A returned value can go anywhere a value fits, even without a variable: System.out.println(area(5, 4)); prints 20, and area(5, 4) * 2 is 40.

Any type can come back

An average has decimals, and a pass check is a plain yes or no. The return type can be any type: double for the average, boolean for the check, String for a word.

Example 2 · A double and a boolean come back

Results.javaJava
public class Results {    public static void main(String[] args) {        double avg = average(78, 85, 90);        System.out.println("Average: " + avg);        if (isPass(avg)) {            System.out.println("Result: Pass");        }    }
    static double average(int m1, int m2, int m3) {        return (m1 + m2 + m3) / 3.0;    }
    static boolean isPass(double avg) {        return avg >= 35;    }}
Output
Average: 84.33333333333333
Result: Pass

What this code does

  • average returns a double; dividing by 3.0 keeps the decimals, as in Division: int or double? (opens in a new tab).
  • isPass returns the comparison itself: avg >= 35 is already true or false, so no if is needed inside it.
  • A boolean call fits straight into an if: if (isPass(avg)) reads almost like English.

void or a return type?

Printing a bill only does a job; working out GST gives an answer that other code needs. Use void for a job, and a return type for an answer.

Classify

Pick the return type

Tap a method, then tap what it should give back.

A void method gives nothing back, so int x = printBill(); stops with incompatible types: void cannot be converted to int.

return ends the method

A grade method checks marks >= 75, then marks >= 35. Java leaves the method at the first return it reaches, so the later checks never run and no else is needed.

Trace the code

Watch return end the method

public class Grade {
public static void main(String[] args) {
String g = grade(60);
System.out.println("Grade: " + g);
}
static String grade(int marks) {
if (marks >= 75) {
return "Distinction";
}
if (marks >= 35) {
return "Pass";
}
return "Fail";
}
}

Variables

None yet.

Output

(nothing printed yet)

Line 3main calls grade(60). Java jumps into grade and waits for its answer.

Step 1 of 7
Every path must end in a return: without the last return "Fail";, Java says missing return statement. A line right after a return can never run, so Java gives unreachable statement.

Next step

Check your understanding

10 quick questions