Java Integer包装类:为何打印结果并非内存地址?
Java中的int
是基本数据类型,直接存储数值;而Integer
是int
的包装类,用于存储int
类型的对象。
当我们声明Integer a = 1;
时,系统会创建一个Integer
对象,其值为1。然而,使用System.out.println(a);
并不会打印对象的内存地址。这是因为Integer
类重写了toString()
方法,该方法返回对象的字符串表示形式,即"1"。
所以,System.out.println(a);
输出的是字符串"1",而不是内存地址。这与自动拆箱无关,因为在调用toString()
方法之前,a
并没有被自动拆箱成int
类型。