Return a value from a method
22 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)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.
Example 1 · Return the area, then use it
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; }}Area: 120 sq ft Tiles: Rs. 5400
What this code does
static int area(…):intin place ofvoidpromises that every call gives back anint.return length * width;sends 120 back. The callarea(12, 10)takes that value, and it is stored infloor.- Now
maincan calculate with it:floor * 45is 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
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; }}Average: 84.33333333333333 Result: Pass
What this code does
averagereturns adouble; dividing by3.0keeps the decimals, as in Division: int or double? (opens in a new tab).isPassreturns the comparison itself:avg >= 35is alreadytrueorfalse, so noifis needed inside it.- A
booleancall fits straight into anif: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.
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
Variables
None yet.
Output
(nothing printed yet)Line 3main calls grade(60). Java jumps into grade and waits for its answer.
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