Review:   Method Signature

The (1) method name and the (2) parameter list together constitutes the method signature:

The method signature is used to identify a method

Therefore:   a class cannot have more than 1 method with the same method signature.

Method overloading

  • A method is overloaded when:

    • There are multiple methods inside a class that have the same method name

      (but have different method signature)

    Example:

        public static void print(short x)
        {
            System.out.println("short: " + x);
        }
        
        public static void print(int x)
        {
            System.out.println("int: " + x);
        }
        
        public static void print(double x)
        {
            System.out.println("double: " + x);
        } 

How Java select methods:   (1) method signatures match exactly

  • The Java compiler determines which method to use by matching the method signatures:

    • Java will select/use a method with the exact matching signature as the arguments if one can be found

    Examples:  

        public static void main(String[] args)
        {
            short  a = 1;
            int    b = 1;
            double c = 1;
    
            print(a);      // Invokes  print(short x)
            print(b);      // Invokes  print(int x)
            print(c);      // Invokes  print(double x)
        }

DEMO: demo/06-methods/05-method-signature/ExactMatch.java     ---     Step to show diff method invocations

How Java select methods:   (2) no exact matching method signatures

  • If no method with exact matching method signature can be found:

    • Java will promote the data type of the arguments to the nearest "larger" type until a matching method signature can be found

                                char
       Data type                  \
       promotion:   byte -> short --> int -> long -> float -> double  

    Examples:

     byte a = 1;    
     print(a);    // Promotion: byte --> short  to find print(short x)
                  // Invokes    print(short x)  (not print(int x))
    
     float c = 1; 
     print(c);    // Promotion: float --> double  to find print(double x)
                  // Invokes    print(double x)

DEMO: demo/06-methods/05-method-signature/NoExactMatch.java

Method selection ambiguity

  • Method selection ambiguity:

    • If there are different ways to find a matching method signature, Java will report an error

  • Example of an ambigious selection:

        public static void main(String[] args) 
        {
           int a = 4, b = 5;
           
           print(a,b); // (1) promote b to double --> uses print(int, double)
                       // (2) promote a to double --> uses print(double, int)
        }
        
        public static void print(int x, double y)
        {
            System.out.println("int x:" + x + " double: " + y);
        }
        
        public static void print(double x, int y)
        {
            System.out.println("double: " + x + " int: " + y);
        } 

DEMO: demo/06-methods/05-method-signature/AmbiguousSelection.java

Good programming practice

  • Recommendation:

    • Do not allow Java to select a method by type promotion

  • Force the choice of the methods by yourself using casting:

        public static void main(String[] args) 
        {
           int a = 4, b = 5;
           
           print((double)a, (int)b);  // Calls: print(double, int)
        }
        
        public static void print(int x, double y)
        {
            System.out.println("int x:" + x + " double: " + y);
        }
        
        public static void print(double x, int y)
        {
            System.out.println("double: " + x + " int: " + y);
        } 

DEMO: demo/06-methods/05-method-signature/MakeExplicit.java