For a similar function method, because the parameter list is not the same, but you need to remember so many different ways to name, too much trouble.
Method overloading (Overload) : Like the names of multiple methods, but not the same parameter list.
Benefits : You only need to remember only one method name, you can achieve a similar multiple functions.
Method overloading associated with the following factors:
1. Depending on the number of parameters
2. The parameters of different types of
3 different types of multi-order parameter
Method overloading regardless of the following factors:
1. the independent parameter name
2. and method return value independent of the type
. 1 public class DemoMethodOverload { 2 . 3 public static void main (String [] args) { . 4 / * System.out.println (sumTwo (10, 20 is)); // 30 . 5 System.out.println (sumThree (10, 20 is , 30)); // 60 . 6 System.out.println (sumFour (10, 20 is, 30, 40)); // 100 * / . 7 . 8 System.out.println (SUM (10, 20 is)); // the method of two parameters . 9 System.out.println (SUM (10, 20 is, 30)); // the three parameters of the method 10 System.out.println (SUM (10, 20 is, 30, 40)); // method four parameters 11 // System.out.println (SUM (10, 20, 30, 40, 50)); // could not find any way to match, so wrong! 12 is 13 is SUM (10, 20 is ); 14 } 15 16 public static int SUM ( int A, Double B) { . 17 return ( int ) (A + B); 18 is } . 19 20 is public static int SUM ( Double A, int B ) { 21 is return ( int ) (A + B); 22 is } 23 is 24 public static int SUM ( int A, int B) { 25 System.out.println ( "method has two parameters is performed!" ); 26 is return A + B; 27 } 28 29 // Error writing! And the return value independent of the type 30 // public static Double SUM (A int, int B) { 31 is // return A + B + 0.0; 32 // } 33 is 34 is // Error writing! Regardless of the name of the parameter 35 // public static int SUM (X int, int Y) { 36 // return X + Y; 37 [ // } 38 is 39 public static int SUM ( Double A, Double B) { 40 return ( int ) (A + B); 41 is } 42 is 43 is public static int SUM ( int A, int B, int C) { 44 is the System .out.println ( "there are three parameters to perform a method!" ); 45 return a + B + C; 46 is } 47 48 public static int SUM (int A, int B, int C, int D) { 49 System.out.println ( "There are four methods of performing parameter!" ); 50 return A + B + C + D; 51 is } 52 is 53 is }
Topics requirements:
Compares two data are mutually equal.
Parameter Type byte are two types, two types of short, two int type, two types of long,
and tested in the main method.
1 public class Demo02MethodOverloadSame { 2 3 public static void main(String[] args) { 4 byte a = 10; 5 byte b = 20; 6 System.out.println(isSame(a, b)); 7 8 System.out.println(isSame((short) 20, (short) 20)); 9 10 System.out.println(isSame(11, 12)); 11 12 System.out.println(isSame(10L, 10L)); 13 } 14 15 public static Boolean isSame ( byte A, byte B) { 16 System.out.println ( "method of performing two byte parameters!" ); . 17 Boolean Same; 18 is IF (A == B) { . 19 Same = to true ; 20 is } the else { 21 is Same = to false ; 22 is } 23 is return Same; 24 } 25 26 is public static Boolean isSame ( ShortA, short B) { 27 System.out.println ( "Method two short execution parameters!" ); 28 Boolean Same == B = A? to true : to false ; 29 return Same; 30 } 31 is 32 public static Boolean isSame ( int a, int B) { 33 is System.out.println ( "method of performing two parameters int!" ); 34 is return a == B; 35 } 36 37 [ public static Boolean isSame ( LongA, long B) { 38 is System.out.println ( "Method two long execution parameters!" ); 39 IF (A == B) { 40 return to true ; 41 is } the else { 42 is return to false ; 43 is } 44 is } 45 46 is }
Determining whether the method can reload
. 1 public class Demo03OverloadJudge { 2 public static void Open () {} // correct overloaded . 3 public static void Open ( int A) {} // correct overloaded . 4 static void Open ( int A, int B) {} // error Code: 8 line and conflicts . 5 public static void Open ( Double A, int B) {} // correct overloaded . 6 public static void Open ( intA, Double B) {} // Code Error: 6, line and conflicts . 7 public void Open ( int I, Double D) {} // Code Error: conflict and the fifth row . 8 public static void the OPEN () {} / / code is not correct the error, but not effective overload . 9 public static void Open ( int I, int J) {} // Code error: conflict and third row 10 }