IKM 某java题引发的思考,求大神指点?? Integer和普通的Object的区别

来源:互联网 发布:js图片上传裁剪插件 编辑:程序博客网 时间:2024/06/06 01:10
package sort;public class Main {public static void main(String[] args) {Integer a = new Integer(1);Integer b = a;System.out.println(a==b);System.out.println("b"+b+" "+b.hashCode());b++;System.out.println(a==b);System.out.println("a"+a+" "+a.hashCode());System.out.println("b"+b+" "+b.hashCode());Data d1 = new Data(1);Data d2 = d1;d2.n++;System.out.println("\n"+d1.n);}}class Data{public int n = 0;public Data(int n){this.n = n;}}


运行结果是:

true
b1 1
false
a1 1
b2 2


2



Integer和我自己实现的对象Data都包含一个int,但是为什么当两个reference指向同一个变量的时候,
Data指向的是同一个对象,Integer在++以后貌似就不是同一个对象了??

请问Integer的++是怎么实现的?

0 0
原创粉丝点击