What is the result of the following statements?
1: public class Main { 2: public void print(byte x) { 3: System.out.print("byte"); 4: } 5: public void print(int x) { 6: System.out.print("int"); 7: } 8: public void print(float x) { 9: System.out.print("float"); 10: } 11: public void print(Object x) { 12: System.out.print("Object"); 13: } 14: public static void main(String[] args) { 15: Main t = new Main(); 16: short s = 12; 17: t.print(s); 18: t.print(false); 19: t.print(6.7); 20: } 21: }
E.
s
is a short and can be promoted to an int, so print() on
line 5 is invoked.
line 18 invokes a boolean. It is autoboxed to a boolean, so print() on line 11 is invoked.
line 19 is a double and is autoboxed to a double, so print() on line 11 is invoked.
public class Main { public void print(byte x) { System.out.print("byte"); }/* w w w . j a v a 2s. c o m*/ public void print(int x) { System.out.print("int"); } public void print(float x) { System.out.print("float"); } public void print(Object x) { System.out.print("Object"); } public static void main(String[] args) { Main t = new Main(); short s = 1; t.print(s); t.print(true); t.print(6.7); } }