Java Reference
In-Depth Information
Consider the following statements:
System.out.println("The larger of 5 and 6 is "
+ larger(5, 6));
//Line 1
System.out.println("The larger of " + firstNum
+ " and " + secondNum + " is "
+ larger(firstNum, secondNum));
//Line 2
System.out.println("The larger of " + firstNum
+ " and 29 is " + larger(firstNum, 29)); //Line 3
maxNum = larger(38.45, 56.78); //Line 4
￿ The expression larger(5, 6) , in Line 1, is a method call, and 5 and 6 are
actual parameters. This statement outputs the larger of 5 and 6 , which is 6 .
￿ The expression larger(firstNum, secondNum) ,inLine2,isamethod
call. Here, firstNum and secondNum are actual parameters. This statement
outputs the larger of firstNum and secondNum ,whichis 36 .
￿ The expression larger(firstNum, 29) , in Line 3, is also a method call.
Here, firstNum and 29 are actual parameters.
￿ The expression larger(38.45, 56.78) , in Line 4, is a method call. In
this call, the actual parameters are 38.45 and 56.78 . In this statement, the
value returned by the method larger is assigned to the variable maxNum .
In a method call, you specify only the actual parameter, not its data type. For example,
in Example 7-2, the statements in Lines 1, 2, 3, and 4 show how to call the method
larger with the actual parameters. However, the following statements contain incorrect
calls to the method larger and would result in syntax errors. (Assume that all variables
are properly declared.)
x = larger( int one, 29); //illegal
y = larger( int one, int 29); //illegal
System.out.println(larger( int one, int two));
//illegal
Final Program
You now know enough to write the entire program, compile it, and run it. The following
program uses the method larger and main to determine the larger of two numbers:
//Program: Larger of two numbers
import java.util.*;
public class LargerNumber
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
 
 
 
Search WWH ::




Custom Search