首先看下面这个例子:

public class TestNew {
public static void main(String args[]){
Integer i1 = 10;
//Integer i1 = Integer.valueOf(10);
Integer i2 = 10;
Integer i3 = 1000;
Integer i4 = 1000;
System.out.println(i1 == i2);
System.out.println(i3 == i4);
}
}

其中注释的地方是用来表示编译器优化后代码的样子,说明应该对于i1 = 10的赋值,由于是包装类,所以要创建一个Integer对象,这样,最后两个对象的地址比较,肯定结果都是false,但是真实的结果并不是这样。

true
false Process finished with exit code 0

第一个比较的结果竟然是true,为了弄清楚原因,就要看Integer的valueOf方法的具体实现了。

   /**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/
public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}

从这里可以看出,对于value在-128到127之间的int值,在调用这个方法创建Integer对象时,是直接从缓冲池中返回的,如果缓冲池中有,则返回的是同一个对象。

那如果此时,对i2赋值为1,是否i1也会变为1呢?

答案是否定的,上一篇文章中的包装类的参数传递问题和这个类似,因为将1赋值给i2,这时候1被自动装箱为Integer对象,并让i2指向这个新对象,所以此时不会改变i

的值。

【Java基础】Integer包装类的缓冲池问题的更多相关文章

  1. java基础-Integer类常用方法介绍

    java基础-Integer类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的.而程序开发中,我们需 ...

  2. java基础数据类型包装类

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  3. java中Integer包装类的具体解说(java二进制操作,全部进制转换)

    程序猿都非常懒,你懂的! 今天为大家分享的是Integer这个包装类.在现实开发中,我们往往须要操作Integer,或者各种进制的转换等等.我今天就为大家具体解说一下Integer的使用吧.看代码: ...

  4. 再学Java 之 Integer 包装类缓存

    前言:本博文将涉及的Java的自动装箱和自动拆箱,可以参考 这篇文章 和 官方教程 ,这里不再赘述. 首先,先看一个小程序: public class Main { public static voi ...

  5. java基础(17):包装类、System、Math、Arrays、大数据运算

    1. 基本类型包装类 大家回想下,在第三篇文章中我们学习Java中的基本数据类型时,说Java中有8种基本的数据类型,可是这些数据是基本数据,想对其进行复杂操作,变的很难.怎么办呢? 1.1 基本类型 ...

  6. Java基础教程——包装类

    Java出道之时,自诩为"纯面向对象的语言",意思是之前的所谓"面向对象语言"不纯. 但是,有人指责Java也不纯--8种基本类型并非类类型.为此,Java为他 ...

  7. Java基础系列--包装类

    原创作品,可以转载,但是请标注出处地址http://www.cnblogs.com/V1haoge/p/5462489.html 1.8种基本数据类型都有各自的包装类,其对应关系为: 基本—————— ...

  8. Java基础学习-包装类

    package packaging; /*包装类: 是封装了基本数据类型的类,为了提供更多复杂方法 Integer: String---int 1.intvalue() 2.parseTnt() in ...

  9. 【JDK】:java.lang.Integer源码解析

    本文对JDK8中的java.lang.Integer包装类的部分数值缓存技术.valueOf().stringSize().toString().getChars().parseInt()等进行简要分 ...

随机推荐

  1. python with关键字学习

    1.with语句时用于对try except finally 的优化,让代码更加美观, 例如常用的开发文件的操作,用try except finally 实现: f=open('file_name', ...

  2. linux sed命令学习

    . Sed简介 . 定址 . Sed命令 . 选项 . 元字符集 . 实例 . 脚本 . 小技巧 . Sed简介 sed是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中, ...

  3. entity framework in mysql

    To start using Entity Framework 6 and Visual Studio 2013 is necessary to install MySQL for Visual St ...

  4. cocos2dx3.4 保存json文件

    头文件: #include "json/document.h" #include "json/stringbuffer.h" #include "js ...

  5. SWFUpload 中文乱码问题

    解决办法:两种: 第一种:把handlers.js的编码方式改为UTF-8(用记事本打开,选择编码格式为utr-8即可) 第二种:在有swfupload控件页面的page_load种加: Respon ...

  6. [转载]MongoDB学习(三):MongoDB Shell的使用

    MongoDB shell MongoDB自带简洁但功能强大的JavaScript shell.JavaScript shell键入一个变量会将变量的值转换为字符串打印到控制台上. 下面介绍基本的操作 ...

  7. Es索引优化

    https://www.elastic.co/guide/en/elasticsearch/guide/current/hardware.html https://www.elastic.co/gui ...

  8. POJ2402+模拟

    题意:找出第index 个回文数. 这题是有规律的,即list[]数组. 其次,对于某个 index 可以先精确到 位数 pos,然后在进行分析. 1a #include<stdio.h> ...

  9. Android ImageButton | Button | TextView 点击和触摸效果

    ImageButton------------------------------------------------> 一.Java  代码: 在drawable目录下添加新的xml文件 bu ...

  10. python描述符descriptor(二)

    python内置的描述符 python有些内置的描述符对象,property.staticmethod.classmethod,python实现如下: class Property(object): ...