谁能告诉我,为什么在内部工作中会打印“int”。
class Test {
void Method(int i) {
System.out.println("int");
}
void Method(String n) {
System.out.println("string");
}
public static void main(String [] a) {
new Test().Method('c');
}
}
最佳答案
字符 c
被转换为 int
。它不会被转换为 String
。
更正式地说,来自 the JLS §5.5 - 从 char
到 int
、long
、float
或 double 的扩展转换
。
永远不会被视为 String
的原因是 char
不能自动装箱到 String
中。
关于Java函数重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17549897/