class Demo {
public void myMethod(int a, double b, char c) {
System.out.println("Version 1.");
}
public void myMethod(int a, double b, double c) {
System.out.println("Version 2.");
}
public void myMethod(int a, boolean b, double c) {
System.out.println("Version 3.");
}
public void myMethod(int a, boolean b, boolean c) {
System.out.println("Version 4.");
}
public static void main(String[] args) {
Demo obj = new Demo();
obj.myMethod(1, 100, 'b');
}
}
1 和 100 都是 int 类型,而 b 是 char 类型,所以结果不应该是编译错误吗?我不明白为什么它返回“版本 1”。
有人可以向我解释为什么吗?
谢谢
最佳答案
值 100 可以用作 double ,它是加宽基元 conversions :
byte to short, int, long, float, or double
short to int, long, float, or double
char to int, long, float, or double
int to long, float, or double
long to float or double
float to double
关于java - 为什么这个方法返回一个输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50316012/