问题遇到的现象和发生背景
定义了一个TargetClazz类
public class TargetClazz {
int num1;
String str1;
Integer num2;
private TargetClazz(String str1, int num1) {
this.num1 = num1;
this.str1 = str1;
System.out.println("int");
}
public TargetClazz(String str1, Integer num2) {
this.str1 = str1;
this.num2 = num2;
System.out.println("integer");
}
public TargetClazz() {
}
}
然后另写一个类new两个对象,部分代码如下:
Integer a = 1;
int b = 2;
TargetClazz aa = new TargetClazz("aa", b);
TargetClazz bb = new TargetClazz("aa", a);
运行结果及报错内容
结果打印出来如下结果:
我的解答思路和尝试过的方法
由于不清楚JVM内部到底是怎么实现的,也尝试过使用 a.intValue() 来代替 a 创建 bb 对象,但打印出来的结果都是integer,因此甚是不解,希望能给解答一下