What is the return type of a method that does not return any value?
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: Show 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.Now suppose that you have a method declared to return a 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 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.
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 TypeA 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 Check your understanding
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()
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 Coding Exercise Write a function 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. What is a return type of method if the method does not return a value?If a method does not return a value, it must be declared to return void .
What is a return type of the method that does not return any value in Java int?What is the return type of a method that does not returns any value? a) intb) floatc) voidd) doubleView AnswerAnswer: cExplanation: Return type of an method must be made void if it is not returning any value. 3.
|