The Java Program: Numeric.java

  1 // Numeric.java:  overloading of methods and numeric promotion
  2 
  3 public class Numeric {
  4 
  5    static void meld (long l, int i) {}    // version 1
  6    static void meld (int i, long l) {}    // version 2
  7    static void meld (char c, int i) {}    // version 3
  8 
  9    static void main (String[] args) {
 10 
 11       long l = 0;
 12       int i  = 1;
 13       char c = 0;
 14       byte b = 1;
 15 
 16       /*
 17         In the next call, version 1 matches exactly.
 18       */
 19       meld (l, i);
 20 
 21       /*
 22         In the next call, only version 2 applies.
 23       */
 24       meld (c, l);
 25 
 26 
 27       /*
 28         All three versions apply to the next call.  Version 1 and 2 are
 29         less specific than the third version, hence they are eliminated.
 30       */
 31       meld (c, b);
 32 
 33       /*
 34         Versions 1 and 2 apply to the next call.  Neither version is less
 35         specific than the other. The call is ambiguous, hence illegal.
 36       meld (i, i);
 37       */
 38 
 39       meld (i, (long) i);   // A cast resolves the ambiguity
 40    }
 41 }