1. 源码

/*** 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) {if (i >= IntegerCache.low && i <= IntegerCache.high)return IntegerCache.cache[i + (-IntegerCache.low)];return new Integer(i);}

2. 测试

public static void main(String[] args) {Integer a0 = 128; // 相当于Integer.valueOf(128)Integer a = Integer.valueOf(128);Integer b = Integer.valueOf(128);System.out.println(a == b); // falseSystem.out.println(a0 == b); // falseSystem.out.println(128 == new Integer(128)); // Integer自动拆箱,trueInteger c = Integer.valueOf(127);Integer d = Integer.valueOf(127);System.out.println(c == d); // true
}

3. 总结

  • int类型比较,相等;
  • 两个Integer比较, 在[-128, 127]内相等,否则不相等,因为会重新new一个对象;
  • 清楚自动装箱(Integer a0 = 128)和自动拆箱(int a = Integer.valueOf(128));

Integer缓存范围-128 to 127相关推荐

  1. Integer缓存问题分析

    Integer缓存问题分析 <阿里巴巴代码规约>中OOP规约部分,第七条:[强制]所有整型包装类对象之间值的比较,全部使用 equals 方法比较. 说明:对于 Integer var = ...

  2. 【java】java Integer 缓存 一定是 -128~127 吗

    1.概述 我们都知道 java Integer是有缓存的,缓存范围是 -128~127.但是这个值是一定的吗? 不一定.看源码.缓存大小居然可以改变 /*** Cache to support the ...

  3. Integer的自动拆装箱的陷阱(整型数-128到127的值比较问题)

    public static void main(String[] a) {test1(); }static void test1() {// Integer的自动拆装箱的陷阱(整型数-128到127的 ...

  4. Integer缓存池

    new Integer(127)和Integer.valueOf(127)是有区别的:理由如下! new Integer(127):每次都会创建一个新的对象. Integer.valueOf(127) ...

  5. Int里-128到127的学问

    转:http://blog.csdn.net/chengzhezhijian/article/details/9628251 最近在项目中遇到一个问题,两个值相同的Integer型值进行==比较时,发 ...

  6. Integer缓存IntegerCache详解

    版权声明 本文原创作者:谷哥的小弟 作者博客地址:http://blog.csdn.net/lfdfhl 引子 今天,我们从一段非常简单的代码说起. 示例代码 package cn.com; /** ...

  7. Integer缓存策略

    先看一段代码 @Testvoid contextLoads() {Integer a=10;Integer b=10;System.out.println(a==b); //trueInteger c ...

  8. java字面量和符号引用_java中字面量,常量和变量之间的区别(附:Integer缓存机制)...

    一.引子 在各种教科书和博客中这三者经常被引用,今天复习到内存区域,想起常量池中就是存着字面量和符号引用,其实这三者并不是只在java中才有,各个语言中都有类似的定义,所以做一下总结,以示区分. 二. ...

  9. 8位alu运算器(vhdl语言)_C语言中signed char类型,能表示-128到127,为什么负数多一位?...

    初学者在学习C语言,谈到不同数据类型时,一般都能理解 unsigned 和 signed 的区别,无非就是有无符号而已.但是对于 signed 数据类型的数据范围,初学者却常常会感到迷惑. 对于 si ...

最新文章

  1. Linux 小知识翻译 - 「命令行的提示符」
  2. R语言效用分析 ( 效能分析、Power analysis)确定样本量、假设检验与两类错误、pwr包进行效用分析 ( 效能分析、Power analysis)的常用函数列表
  3. target tile component.js load logic
  4. 【点分治】Tree(luogu 4178/金牌导航 点分治-1)
  5. 键盘录入,替换敏感词
  6. diff算法阮一峰_【重学数据结构与算法(JS)】字符串匹配算法(三)——BM算法
  7. java并发编程之线程的生命周期详解
  8. CallStack获取函数堆栈
  9. 网络入门-TRANK
  10. react后台管理项目
  11. 2006年网络名言100句(转)
  12. Swagger之OAS 3.0配置
  13. python实现大规模邻域搜索(LNS)求解旅行商问题(TSP)
  14. 推动企业上云实施经验总结
  15. 搭建属于自己的数字IC EDA环境(六):开机自动激活 Synopsys license
  16. veins安装及运行笔记
  17. PowerPC PPC460-S MMU(六 Shadow TLB)
  18. 浅谈氢能源三大竞争优势
  19. VMware Workstation 安装centOS
  20. 物业管理信息系统的研究与实现

热门文章

  1. 邵阳python编程培训机构
  2. 查全率(precison)和查准率(recall)
  3. js实现分数计算器的代码
  4. OSChina 周五乱弹 —— 来来来,我给你看个宝贝
  5. 昇腾A200DK【Mindstudio】推理测试
  6. 介绍北京演示文稿计算机二级,计算机二级PPT真题:制作北京景点介绍PPT
  7. (邮件/用户)代理协议简介Socket程序发送电子邮件
  8. C# 自动更新(基于FTP)
  9. 为什么L1惩罚L2惩罚更容易得到稀疏解
  10. 【Web技术】1295- 总结一下前端本地储存方案