今天在一个java群里,看到有个群友问到如下为什么第一个为true,第二个为false。  

  System.out.println(Integer.valueOf("50")==Integer.valueOf("50"));   //true
  System.out.println(Integer.valueOf("200")==Integer.valueOf("200"));  //false

  由于一开始他问的第二句,我还想当然的以为是new的对象,肯定不一样,但是为什么第一句为true呢,后来通过查找资料发现

  1、https://www.zhihu.com/question/29879295/answer/102396251  

  2、http://blog.csdn.net/chengzhezhijian/article/details/9628251

    /**
* Returns a <tt>Integer</tt> instance representing the specified
* <tt>int</tt> value.
* If a new <tt>Integer</tt> 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.
*
* @param i an <code>int</code> value.
* @return a <tt>Integer</tt> instance representing <tt>i</tt>.
* @since 1.5
*/
public static Integer valueOf(int i) {
if(i >= -128 && i <= IntegerCache.high)
return IntegerCache.cache[i + 128];
else
return new Integer(i);
}

  

 private static class IntegerCache {
static final int high;
static final Integer cache[]; static {
final int low = -128; // high value may be configured by property
int h = 127;
if (integerCacheHighPropValue != null) {
// Use Long.decode here to avoid invoking methods that
// require Integer's autoboxing cache to be initialized
int i = Long.decode(integerCacheHighPropValue).intValue();
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - -low);
}
high = h; cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
} private IntegerCache() {}
}

  valueOf会将常用的值(-128 to 127)cache起来。当i值在这个范围时,会比用构造方法Integer(int)效率和空间上更好。

  

java关于Integer设置-128到127的静态缓存的更多相关文章

  1. java中Integer与int装箱拆箱一点收获

    示例代码: class BoxIntInteger { public static void main(String[] args) { Integer a = new Integer(10111); ...

  2. Java中Integer类型的整数值的大小比较

    如果比较两个数值相等的Integer类型的整数,我们可能会发现,用"=="比较(首先你必须明确"=="比较的是地址),有的时候返回true,而有的时候,返回fa ...

  3. Java: Integer用==比较时127相等128不相等的原因

    直接看问题吧 for (int i = 0; i < 150; i++) { Integer a = i; Integer b = i; System.out.println(i + " ...

  4. Integer a= 127 与 Integer b = 128相关

    Integer a = 127; Integer b = 127; Integer c = 128; Integer d = 128; a == b 与 c == d 的比较结果是什么? a == b ...

  5. java中为什么byte的取值范围是-128到+127

    概念:java中用补码表示二进制数,补码的最高位是符号位,最高位为“0”表示正数,最高位为“1”表示负数.正数补码为其本身:负数补码为其绝对值各位取反加1:例如:+21,其二进制表示形式是000101 ...

  6. Integer自动装箱拆箱bug,创建对象在-128到127

    1 public class Demo3 { public static void main(String[] args) { Integer a = 1; Integer b = 2; Intege ...

  7. java中byte取值范围为什么是 -128到127

    概念:java中用补码表示二进制数,补码的最高位是符号位,最高位为“0”表示正数,最高位为“1”表示负数.正数补码为其本身:负数补码为其绝对值各位取反加1:例如:+21,其二进制表示形式是000101 ...

  8. java.lang.Integer源码浅析

    Integer定义,final不可修改的类 public final class Integer extends Number implements Comparable<Integer> ...

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

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

随机推荐

  1. iOS - 如何适配iOS10(插曲)

    升级了系统10.12beta xcode8  出现一大推问题 ,连上架APP都成了问题.只能先解决这些问题,再研究3D引擎了. 2016年9月7日,苹果发布iOS 10.2016年9月14日,全新的操 ...

  2. JavaScript中对null和undefined的理解

    前沿: 今天工作中遇到了监视一个变量是undefined,结果判断写的是==null 返回值是true,这个结果引起了我对这两个东西的兴趣. 查询了相关的文章理解并测试了.发现有以下特点: 1.广义上 ...

  3. iOS 开发之模糊效果的五种实现

    前言 在iOS开发中我们经常会用到模糊效果使我们的界面更加美观,而iOS本身也提供了几种达到模糊效果的API,如:Core Image,使用Accelerate.Framework中的vImage A ...

  4. Nginx 安装目录 和 编译参数

    安装目录详解 查看安装nginx之后总共生成了哪些文件 rpm -ql nginx 在上面的文件中包括配置文件和日志文件 /etc/logrotate.d/nginx 类型:配置文件 作用:Nginx ...

  5. mongodb副本集和分片存储理论整理

    目录 理论概述 一.各种集群简述 二.原理 主从复制 Mongodb副本集 理论概述 一.各种集群简述 mongodb有三种集群搭建方式: 分片:sharding.指为处理大量数据,将数据分开存储,不 ...

  6. Jenkins管理插件

    1 配置自动更新的镜像 https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json 系统管理-----插件管理--- ...

  7. selenium xpath定位方式整理

    #xpath定位元素方法: /html/body/div[2] #绝对路径定位 #相对路径定位元素 //* #找到所有的元素 //input #找到input元素 //*[@*] #表示有属性的所有元 ...

  8. master-worker常驻型程序代码修改哪些需要重启master或者worker

    之前在yii的项目里用redis作为消息队列,现在很多任务需要延迟需求,于是把之前redis的消息队列替换成了rabbitmq 于是使用yii的yii2-queue这个组件 但是由于提供的yii qu ...

  9. Docker那些事儿之镜像创建

    之前已经了解了docker的基本使用方式,简单的上手,也能让大部分人了解到这个技术的使用方法,今天继续说明docker如何构建自己所需要的镜像,开发人员掌握使用基础即可,有兴趣的可以自行深入研究 前言 ...

  10. flutter,flutter版本version/channel切换问题

    flutter go,官方的指南版本如下: 如何设置版本和channel,尝试 flutter help,发现原来flutter version不单是可以查所有版本(--version查当前版本)还可 ...