The max method that was used earlier works only with the int data type. But what if you need to find which of two floating-point numbers has the maximum value? The solution is to create another method with the same name but different parameters, as shown in the following code:
public static double max( double num1, double num2) { if (num1 > num2) return num1; else return num2; }
If you call max with int parameters, the max method that expects int parameters will be invoked; if you call max with double parameters, the max method that expects double parameters will be invoked. This is referred to as method overloading ; that is, two methods have the same name but different parameter lists within one class. The Java compiler determines which method is used based on the method signature.
Listing 5.4 is a program that creates three methods. The first finds the maximum integer, the second finds the maximum double, and the third finds the maximum among three double values. All three methods are named max . The output of the program is shown in Figure 5.7.
1 public class TestMethodOverloading { 2 /** Main method */ 3 public static void main(String[] args) { 4 // Invoke the max method with int parameters 5 System.out.println( "The maximum between 3 and 4 is " 6 + max( 3 , 4 ) ); 7 8 // Invoke the max method with the double parameters 9 System.out.println( "The maximum between 3.0 and 5.4 is " 10 + max( 3.0 , 5.4 ) ); 11 12 // Invoke the max method with three double parameters 13 System.out.println( "The maximum between 3.0, 5.4, and 10.14 is " 14 + max( 3.0 , 5.4 , 10.14 ) ); 15 } 16 17 /** Return the max between two int values */ 18 public static int max( int num1, int num2) { 19 if (num1 > num2) 20 return num1; 21 else 22 return num2; 23 } 24 25 /** Find the max between two double values */ 26 public static double max( double num1, double num2) { 27 if (num1 > num2) 28 return num1; 29 else 30 return num2; 31 } 32 33 /** Return the max among three double values */ 34 public static double max( double num1, double num2, double num3) { 35 return max(max(num1, num2), num3); 36 } 37 } |
When calling max(3, 4) (line 6), the max method for finding the maximum of two integers is invoked. When calling max(3.0, 5.4) (line 10), the max method for finding the maximum of two doubles is invoked. When calling max(3.0, 5.4, 10.14) (line 14), the max method for finding the maximum of three double values is invoked.
Can you invoke the max method with an int value and a double value, such as max(2, 2.5) ? If so, which of the max methods is invoked? The answer to the first question is yes. The answer to the second is that the max method for finding the maximum of two double values is invoked. The argument value 2 is automatically converted into a double value and passed to this method.
You may be wondering why the method max(double, double) is not invoked for the call max(3, 4) . Both max(double, double) and max(int, int) are possible matches for max(3, 4) . The Java compiler finds the most specific method for a method invocation. Since the method max(int, int) is more specific than max(double, double) , max(int, int) is used to invoke max(3, 4) .
Tip
Overloading methods can make programs clearer and more readable. Methods that perform closely related tasks should be given the same name. |
Note
Overloaded methods must have different parameter lists. You cannot overload methods based on different modifiers or return types. |
Note
Sometimes there are two or more possible matches for an invocation of a method, but the compiler cannot determine the most specific match. This is referred to as ambiguous invocation . Ambiguous invocation causes a compilation error. Consider the following code: public class AmbiguousOverloading { public static void main(String[] args) { System.out.println( max( 1 , 2 ) ); } public static double max(int num1, double num2) { if (num1 > num2) return num1; else return num2; } public static double max(double num1, int num2) { if (num1 > num2) return num1; else return num2; } } Both max(int, double) and max(double, int) are possible candidates to match max(1, 2) . Since neither of them is more specific than the other, the invocation is ambiguous, resulting in a compilation error. |