The isEmpty
method returns a primitive type. A method can return a reference type. For example, Stack
declares the
public Object pop[] { if [top == 0] { throw new EmptyStackException[]; } Object obj = items[--top]; items[top]=null; return obj; }6 method that returns the
public Object pop[] { if [top == 0] { throw new EmptyStackException[]; } Object obj = items[--top]; items[top]=null; return obj; }7 reference type:
When a method uses a class name as its return type, such aspublic Object pop[] { if [top == 0] { throw new EmptyStackException[]; } Object obj = items[--top]; items[top]=null; return obj; }
public Object pop[] { if [top == 0] { throw new EmptyStackException[]; } Object obj = items[--top]; items[top]=null; return obj; }6 does, the class of the type of the returned object must be either a subclass of or the exact class of the return type. Suppose that you have a class hierarchy in which
public Object pop[] { if [top == 0] { throw new EmptyStackException[]; } Object obj = items[--top]; items[top]=null; return obj; }9 is a subclass of
public Number returnANumber[] { ... }0, which is in turn a subclass of
public Object pop[] { if [top == 0] { throw new EmptyStackException[]; } Object obj = items[--top]; items[top]=null; return obj; }7, as illustrated in the following figure.
public Number returnANumber[] { ... }2:
Thepublic Number returnANumber[] { ... }
public Number returnANumber[] { ... }3 method can return an
public Object pop[] { if [top == 0] { throw new EmptyStackException[]; } Object obj = items[--top]; items[top]=null; return obj; }9 but not an
public Object pop[] { if [top == 0] { throw new EmptyStackException[]; } Object obj = items[--top]; items[top]=null; return obj; }7.
public Object pop[] { if [top == 0] { throw new EmptyStackException[]; } Object obj = items[--top]; items[top]=null; return obj; }9 is a
public Number returnANumber[] { ... }2 because it's a subclass of
public Number returnANumber[] { ... }2. However, an
public Object pop[] { if [top == 0] { throw new EmptyStackException[]; } Object obj = items[--top]; items[top]=null; return obj; }7 is not necessarily a
public Number returnANumber[] { ... }2 — it could be a
return
1 or another type.You also can use interface names as return types. In this case, the object returned must implement the specified interface.
You saw examples of methods that return values in Unit 2 with the Turtle
methods [getWidth, getXpos, etc]. The method calls were usually on the right hand side of an assignment, or they were contained in a print statement. When a method returns a value, the code should do something with the value such as store it in a variable or print it.
You will learn how to create methods that access object attributes in a later lesson. This lesson shows you how to create static methods that are functions. A function takes one or more values passed as formal parameters and computes a new value to return.
A method can return at most one value. The method signature specifies the return type, which can be a primitive [int, double, boolean], a class [String, Turtle, etc], or void.
- public
- This is the access modifier.
- static
- This is the non-access modifier.
- double
- Correct. This is the return type.
- volumeCylinder
- This is the method name.
5-3-1: This method computes the volume of a cylinder. The method has formal parameters for radius and height, and returns a value representing the corresponding volume. Given the method signature, what is the return type?
5.3.1. Method Return Type
A void return type means the method does not return a value. If a method has a non-void return type, then it must contain a return statement that specifies the value to return. The return type must match with the value in the return statement.
Click on each tab to observe the data flowing into the method through the formal parameters and out of the method through the return statement.
The program starts at the first line of the main method. The red arrow shows that line 12 is next to execute, which will call the volumeCylinder function.
The stack diagram shows a new frame was created for the volumeCylinder[4,10] method call. The new stack frame contains the formal parameter variables initialized to the actual argument values.
After line 6 is executed, the stack frame shows the computed value 502.6 will be returned out of the method. Where does the returned value go? The call stack diagram indicates the value is returned to line 12 of the main method, since that is the line of code that called the volumeCylinder method.
The value was returned to the main method and line 12 assigns the value to the “vol” local variable. Line 13 is the next line to execute.
Line 13 prints the value stored in the vol local variable. The output window shows what was printed.
Use the CodeLens to step through the code. Experiment with passing different values to the volumeCylinder method.
Coding Exercise
The code below contains a method inchesToCentimeters
that computes and prints the centimeter equivalent of the value passed into the inches parameter. Instead of printing the centimeter value inside the inchesToCentimeters method, you should update the method to return the value and then move the printing to the main method. You will have to change the return type of the inchesToCentimeters method to match the type of the value being returned. Update the main
method to print the value returned by the inchesToCentiments
method.
Check your understanding
- return "hello";
- The method return type int does not match the return statement type String.
- return true;
- The method return type int does not match the return statement type boolean.
- return 7.5;
- The method return type int does not match the return statement type double.
- return 10;
- The method return type int matches the return statement type int.
5-3-4: Based on the method header below, which return statement has the correct type?
5-3-5: Based on the method header below, which return statement has the correct type?
public static boolean mystery2[]
5-3-6: Based on the method header below, which assignment statement is correct?
public static int mystery3[]
5-3-7: Based on the method header below, which statement is correct for calling the method?
public static void mystery4[]
- return 10;
- The method return type int matches the return statement type int.
- return 12 * 4;
- The method return type int matches the return statement type int.
- return 15 / 2;
- The method return type int matches the return statement type int.
- return 3.7 ;
- The method return type int does not match the return statement type double.
5-3-8: Based on the method header below, which return statement DOES NOT have the correct type?
Coding Exercise
A pedometer estimates that taking 2,000 steps is the same as walking 1 mile. Write a method convertToMiles
that takes a parameter for the number of steps and returns the equivalent miles walked. Update the main method to call convertToMiles
3 times with values 500, 2000, 3000. Carefully consider the method return type. Watch out for integer division in the method body! You can assume the number of steps is an integer.
Coding Exercise
Write a function randomInteger
that takes two integer parameters
public static int mystery3[]0 and
public static int mystery3[]1 and returns a random integer value between min and max [inclusive]. Have the main method call the function with different values. You might want to go back and review random number generation in Unit 2-9.