public class Tester {
public Tester(){
System.out.println("Hello");
}
public Tester(byte b){
System.out.println("byte");
}
public Tester(int i){
System.out.println("int");
}
public static void main(String[] args) {
Tester test=new Tester(12);
}
}
Run Code Online (Sandbox Code Playgroud)
请告知为什么打印是int,我也尝试过其他整数,它们都打印为int,但是例如,1 2 3 4 5 6 7 ....这些数字也可以称为byte,对吧?那么为什么只调用int?
默认情况下,除非您将其强制转换为byte,否则它将接受为int
如果你想获得字节,那么按照以下步骤操作
Tester test=new Tester((byte)12);
Run Code Online (Sandbox Code Playgroud)
输出字节