基本类型数据可以从一个较小的类型自动提升为一个较大的类型,但这种特性与重载一起出现时,会是什么样呢?- public class Main{
- static void f1(char x){System.out.print(" f1(char) ");}
- static void f1(byte x){System.out.print(" f1(byte) ");}
- static void f1(short x){System.out.print(" f1(short) ");}
- static void f1(int x){System.out.print(" f1(int) ");}
- static void f1(long x){System.out.print(" f1(long) ");}
- static void f1(float x){System.out.print(" f1(float) ");}
- static void f1(double x){System.out.print(" f1(double) ");}
-
- static void f2(byte x){System.out.print(" f2(byte) ");}
- static void f2(short x){System.out.print(" f2(short) ");}
- static void f2(int x){System.out.print(" f2(int) ");}
- static void f2(long x){System.out.print(" f2(long) ");}
- static void f2(float x){System.out.print(" f2(float) ");}
- static void f2(double x){System.out.print(" f2(double) ");}
-
- static void f3(short x){System.out.print(" f3(short) ");}
- static void f3(int x){System.out.print(" f3(int) ");}
- static void f3(long x){System.out.print(" f3(long) ");}
- static void f3(float x){System.out.print(" f3(float) ");}
- static void f3(double x){System.out.print(" f3(double) ");}
-
- static void f4(int x){System.out.print(" f4(int) ");}
- static void f4(long x){System.out.print(" f4(long) ");}
- static void f4(float x){System.out.print(" f4(float) ");}
- static void f4(double x){System.out.print(" f4(double) ");}
-
- static void f5(long x){System.out.print(" f5(long) ");}
- static void f5(float x){System.out.print(" f5(float) ");}
- static void f5(double x){System.out.print(" f5(double) ");}
-
- static void f6(float x){System.out.print(" f6(float) ");}
- static void f6(double x){System.out.print(" f6(double) ");}
-
- static void f7(double x){System.out.print(" f7(double) ");}
-
- public static void main(String[] args) {
- System.out.print("常量: ");//常量被当作int型
- f1(1);f2(1);f3(1);f4(1);f5(1);f6(1);f7(1);
-
- System.out.print("\nchar: ");//char型,如果找不到接受char型的方法,那么会直接提升为int型
- char c=1;
- f1(c);f2(c);f3(c);f4(c);f5(c);f6(c);f7(c);
-
- System.out.print("\nbyte: ");
- byte b=1;
- f1(b);f2(b);f3(b);f4(b);f5(b);f6(b);f7(b);
-
- System.out.print("\nshort: ");
- short s=1;
- f1(s);f2(s);f3(s);f4(s);f5(s);f6(s);f7(s);
-
- System.out.print("\nint: ");
- int i=1;
- f1(i);f2(i);f3(i);f4(i);f5(i);f6(i);f7(i);
-
- System.out.print("\nlong: ");
- long l=1;
- f1(l);f2(l);f3(l);f4(l);f5(l);f6(l);f7(l);
-
- System.out.print("\nfloat: ");
- float f=1f;
- f1(f);f2(f);f3(f);f4(f);f5(f);f6(f);f7(f);
-
- System.out.print("\ndouble: ");
- double d=1;
- f1(d);f2(d);f3(d);f4(d);f5(d);f6(d);f7(d);
- }
- }
复制代码 输出:
常量: f1(int) f2(int) f3(int) f4(int) f5(long) f6(float) f7(double)
char: f1(char) f2(int) f3(int) f4(int) f5(long) f6(float) f7(double)
byte: f1(byte) f2(byte) f3(short) f4(int) f5(long) f6(float) f7(double)
short: f1(short) f2(short) f3(short) f4(int) f5(long) f6(float) f7(double)
int: f1(int) f2(int) f3(int) f4(int) f5(long) f6(float) f7(double)
long: f1(long) f2(long) f3(long) f4(long) f5(long) f6(float) f7(double)
float: f1(float) f2(float) f3(float) f4(float) f5(float) f6(float) f7(double)
double: f1(double) f2(double) f3(double) f4(double) f5(double) f6(double) f7(double)
可见,若有正符合的类型就是用该类型,若没有,就往上提升,提升的优先顺序:byte->short->int->long->float->double
char除外。 |