今天在一个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. django pk 和id用法

    pk就是primary key的缩写,也就是任何model中都有的主键,那么id呢,大部分时候也是model的主键,所以在这个时候我们可以认为pk和id是完全一样的. class Student(mo ...

  2. centos+docker+jenkins

    1.直接运行jenkins镜像,无该镜像会直接下载 docker run -p 8080:8080 -p 50000:50000 -d -v /home/jenkins-home-docker:/va ...

  3. mysql建表常用命令

    MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的 RD ...

  4. Hbuilder中配置cmd

    步骤: 1. ①运行——>外部工具——>外部工具 配置2.  新建一个名为cmd(随意命名)的外部工具: 名称:cmd 要执行的命令或文件:C:\Windows\System32\cmd. ...

  5. 用BIO手写实现Redis客户端的探究(拒绝Jedis)

    在Redis的使用过程中,大多数人都是使用现成的客户端,如Jedis,Redisson,Lettuce.因此本文研究用BIO的方式手写Redis客户端尝试,对遇到的问题进行探究及总结. Redis通讯 ...

  6. [LeetCode] 1029. 两地调度 ☆(贪心)

    官方题解 作差排序 描述 公司计划面试 2N 人.第 i 人飞往 A 市的费用为 costs[i][0],飞往 B 市的费用为 costs[i][1]. 返回将每个人都飞到某座城市的最低费用,要求每个 ...

  7. C#-FileHelper

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. Flutter——Drawer、DrawerHeader、UserAccountsDrawerHeader组件(侧边栏组件)

    在 Scaffold 组件里面传入 drawer 参数可以定义左侧边栏,传入 endDrawer 可以定义右侧边栏.侧边栏默认是隐藏的,我们可以通过手指滑动显示侧边栏,也可以通过点击按钮显示侧边栏. ...

  9. ant不是内部命令

    解压路径为举例路径:    解压在E盘 新建变量ANT_HOME 路径为解压目录如E:/apache-ant-1.7.1 Path中添加路径为%ANT_HOME%/bin; 错误提示: 'ant' 不 ...

  10. jade成段的文本和标签

    如果文本是大段的文本怎么写呢?因为文本会换行,比如有多行的文本,标签,文本傻傻分不清楚 p 1234567890 => <p>1234567890</p> 如果需要换行, ...