Sub topic 3
Return Results or Use void
A method can produce a result for the rest of a program to use. Its return type tells Java what kind of result to expect. For example, an int return type means the method returns an integer, or whole number.
A return statement sends the result back to the place where the method was called. It also ends the method immediately.
Some methods do a task without giving a result back. They use the return type void instead.
Return a value
The example below calculates a shipping cost. Orders of 50 or more have free shipping; smaller orders have a shipping cost of 5.
Save this program as ShippingDemo.java. Compile it with javac ShippingDemo.java, then run it with java ShippingDemo.
class ShippingDemo {
static int shippingCost(int orderTotal) {
if (orderTotal >= 50) {
return 0;
}
return 5;
}
static void printShipping(int cost) {
System.out.println("Shipping: " + cost);
}
public static void main(String[] args) {
int orderTotal = 40;
int cost = shippingCost(orderTotal);
printShipping(cost);
System.out.println("Total: " + (orderTotal + cost));
System.out.println("Shipping for 60: " + shippingCost(60));
}
}The output is:
- Shipping: 5
- Total: 45
- Shipping for 60: 0
Follow the first call step by step:
- The call to shippingCost sends the value 40 to its orderTotal parameter.
- The condition checks whether 40 is at least 50. It is not, so the method reaches the statement that returns 5.
- The method ends. The returned value, 5, is stored in the cost variable.
- The program uses cost to print the shipping charge and calculate the total.
The later call sends 60 to shippingCost. This time the condition is true, so the method returns 0. It ends at that point and does not reach the statement that returns 5.
Use the returned value
You can store a returned value in a variable, as the program does with cost. You can also use it directly as part of another expression. The last print statement does this with the result of shippingCost(60).
The value after return can itself be an expression. Java works out the expression first, then sends its value back. For example, a method returning an int could return the result of adding two int values.
A method with an int return type must return a value that Java can use as an int. Also, every path that finishes the method normally must return a value. In shippingCost, both possible paths return an int: one returns 0, and the other returns 5.
Use void for a task
The printShipping method has the return type void. It displays a message, but it does not send a result back. The program calls it as a statement on its own, rather than trying to store a result.
Printing and returning are different:
- Printing displays text for a person to see.
- Returning gives a value back to the code that called the method.
A void method cannot return a value. It may use a return statement with no value to end early, but it does not need one at the end. The printShipping method simply finishes after its print statement.
Check your understanding
Exercise
What does shippingCost(50) return, and does it reach the statement that returns 5?
Show answer
It returns 0. The condition is true, and returning ends the method before it reaches the statement that returns 5.
Exercise
Can you store the result of printShipping(5) in an int variable? Why?
Show answer
No. printShipping has the return type void, so it does not provide a value to store.