The Java Program: Promote.java

  1 // Promote.java:  overloading of methods and numeric promotion
  2 
  3 public class Promote {
  4 
  5    static void f (double d) {
  6       System.out.println ("f for double");
  7    }
  8 
  9    static void g (double d) {
 10       System.out.println ("g for double");
 11    }
 12    static void g (int i) {
 13       System.out.println ("g for int");
 14    }
 15 
 16 
 17    static void f (double d1, double d2) {
 18       System.out.println ("f for double, double");
 19    }
 20    static void f (int i, double d) {
 21       System.out.println ("f for int, double");
 22    }
 23 
 24    static void g (double d, int i) {
 25       System.out.println ("g for double, int");
 26    }
 27    static void g (int i, double d) {
 28       System.out.println ("g for int, double");
 29    }
 30 
 31 
 32    
 33    public static void main (String[] args) {
 34 
 35       f (4.5);   f (3);
 36       g (4.5);   g (3);
 37 
 38       /*
 39         f:int×double is more specific than f:double×double,
 40         so f:double×double is eliminated from contention.
 41       */
 42       f (3, 4);
 43       
 44       /*
 45         Both g:int×double and g:double×int are possible,
 46         so the following call is ambiguous and hence illegal.
 47       g (3, 4);
 48       */
 49 
 50       g (3, (double) 4);
 51    }
 52 }