涉及基本类型的重载(之前不知道会自动提升类型,所以摘要记录):
基本类型能从一个“较小”的类型自动提升至一个“较大”的类型,此过程一旦牵涉到重载,可能会造成一些混淆。以下一些例子说明了将基本类型传递给重载方法时发生的情况。(单独把这个摘出来的原因是,以前从来没有考虑过这种情况)。
public f1(char x) {
system.out.println("f1(char)");
}
public f1(byte x) {
system.out.println("f1(byte)");
}
public f1(short x) {
system.out.println("f1(short)");
}
public f1(int x) {
system.out.println("f1(int)");
}
public f1(long x) {
system.out.println("f1(long)");
}
public f1(float x) {
system.out.println("f1(float)");
}
public f1(double x) {
system.out.println("f1(double)");
}
public f2(byte x) {
system.out.println("f2(byte)");
}
public f2(short x) {
system.out.println("f2(short )");
}
public f2(int x) {
system.out.println("f2(int)");
}
public f2(long x) {
system.out.println("f2(long)");
}
public f2(float x) {
system.out.println("f2(float)");
}
public f2(double x) {
system.out.println("f2(double)");
}
public f3(short x) {
system.out.println("f3(short )");
}
public f3(int x) {
system.out.println("f3(int)");
}
public f3(long x) {
system.out.println("f3(long)");
}
public f3(float x) {
system.out.println("f3(float)");
}
public f3(double x) {
system.out.println("f3(double)");
}
public f4(int x) {
system.out.println("f4(int)");
}
public f4(long x) {
system.out.println("f4(long)");
}
public f4(float x) {
system.out.println("f4(float)");
}
public f4(double x) {
system.out.println("f4(double)");
}
public f5(long x) {
system.out.println("f5(long)");
}
public f5(float x) {
system.out.println("f5(float)");
}
public f5(double x) {
system.out.println("f5(double)");
}
public f6(float x) {
system.out.println("f6(float)");
}
public f6(double x) {
system.out.println("f6(double)");
}
public f7(double x) {
system.out.println("f7(double)");
}
void testConstVal(){
system.out.println("5: ");
f1(5);f2(5);f3(5);f4(5);f5(5);f6(5);f7(5);
}
void testChar(){
char x = 'x';
system.out.println("char: ");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
void testByte(){
byte x = 0;
system.out.println("byte: ");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
void testShort(){
short x = 0;
system.out.println("short: ");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
void testInt(){
int x = 0;
system.out.println("int: ");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
void testLong(){
long x = 0;
system.out.println("long: ");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
void testFloat(){
float x = 0;
system.out.println("float: ");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
void testDouble(){
double x = 0;
system.out.println("double: ");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
public static void main(String[] args){
testConstVal();
testChar();
testByte();
testShort();
testInt();
testLong();
testFloat();
testDouble();
}
打印信息:
5: 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)
常数值5被当作int值处理,如果传入的数据类型·(实际参数类型) 小于方法中声明的形式参数类型实际数据类型就会被提升。char类型略有不同,如果无法恰好接受char参数的方法,就会把char提升至int型。