今天在一个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. Centos6 r8192ce_pci

    Centos6 r8192ce_pci # # yum install epel-release# yum-config-manager --enable rhui-REGION-rhel-serve ...

  2. springboot笔记03——quickstart程序原理

    一.前言 一个quickstart程序仅仅让我们初步了解一个框架,我们还需要透过现象看本质才能学好一个框架.所以这篇文章分析一下我上次写的springboot的入门程序. 二.原理分析 1.依赖分析 ...

  3. Spring循环依赖的三种方式以及解决办法

    一. 什么是循环依赖? 循环依赖其实就是循环引用,也就是两个或者两个以上的bean互相持有对方,最终形成闭环.比如A依赖于B,B依赖于C,C又依赖于A.如下图: 注意,这里不是函数的循环调用,是对象的 ...

  4. How to : SAP PI Cache Refresh

    Requirement : Identify various tools/resources available to perform SAP PI Cache refresh . Please no ...

  5. cdh-hbase用户无法执行命令

  6. ubuntu 使用MySQL Workbench 连接远程云服务器mysql

    前提:我的是腾讯云的服务器,所以需要在安全组开发端口. 配置安全组 1.创建新用户 一般为了安全性,我们不直接使用root用户,而是选择创建一个新用户. 在服务器中,输入  mysql -u root ...

  7. Scala 中 call by name & call by value 的区别

    call by value:会先计算参数的值,然后再传递给被调用的函数 call by name:参数会到实际使用的时候才计算 定义方法 def return1():Int = { println(& ...

  8. C# 数值计算、转换

    1.保留小数位 今天再做到计算数值百分比的时候,刚开始试了几个都是不行: , num2 = ; double percent = num2 / num1; , num2 = ; double perc ...

  9. 《AlwaysRun!》第五次作业:项目需求分析改进与系统设计

     项目 内容 这个作业属于哪个课程 2016级软件工程(西北师范大学) 这个作业的要求在哪里 实验九 团队作业5—团队项目需求改进与系统设计 团队名称 Always Run! 作业学习目标 (1)掌握 ...

  10. 04-Dockerfile介绍与使用

    什么是dockerfileDockerfile是由一系列命令和参数构成的脚本,这些命令用一基础镜像并最终创建一个新的镜像.1.对于开发人员:可以为开发团队提供一个完全一致的开发环境.2.对于测试人员: ...