背景

今天在查看Sonar的时候发现小伙伴在某些场景下如下使用

很明显sonar已经报错了,但是线上应用目前是正常的

问题

事实上经常会有面试的小伙伴或者笔试的小伙伴问这个问题

Integer的一些小知识

Integer i2 = Integer.valueOf(1);
Integer i3 = Integer.valueOf(1);
Assert.assertTrue(i2 == i3);

按照大家的理解 valueOf会返回一个Integer 当用Integer和Integer进行比较 这是一个典型的对象比较

通常大家都会使用equals进行 但是此处用==居然可以返回true

@Test
public void testValueOfIntegerIntegerGe128() {
Integer i2 = Integer.valueOf(128);
Integer i3 = Integer.valueOf(128);
Assert.assertFalse(i2 == i3);
}

但是当值为128的时候居然又神奇的失败了~

Integer出现了根据数据不同 同样使用==来做判断却返回了了不同的结果!

分析

我们是否考虑过莫非Integer的小数字对象可以用==进行比较?

根据常识==只能判断是否为同一个对象

在上述代码中该同学对应的代码中

显示的使用setIsLocal进行设置只有这五处。但是有可能该对象是通过Spring的序列化来完成的。因此仍需进行考察!

源码

查看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);
}

很明显存在一个叫做IntegerCache的缓存了相对应的Integer的实例

当调用valueof的时候会优先判断对应是否存在指定区间内 如果确实在对应区间则直接返回对应缓存对象。

这样自然可以使用==来做判断!

private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[]; static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
int i = parseInt(integerCacheHighPropValue);
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() {}
}

ntegerCache会读取vm的参数如果没有设置java.lang.Integer.IntegerCache.high则以127为界!

否则以对应的值作为上界。

重新执行该测试的话

思考

Integer为啥提供该设置呢?

相对来说我们使用数字的时候基本以小数字为主

比如单价 比如个数

如果我们数量等等都要一个新的对象的话那么可能新生代需要回收的就会比较频繁!

那么很明显使用==仍然只是判断对象是否同一个对象而已!

@Test
public void testIntInteger() {
Integer i2 = 1;
Integer i3 = new Integer(1);
Assert.assertFalse(i2 == i3);
} @Test
public void testIntegerInteger() { Integer i2 = new Integer(1);
Integer i3 = new Integer(1);
Assert.assertFalse(i2 == i3);
}

很明显Integer的判断相等仍然需要使用equals进行!!!

面试常问小知识点之Integer的更多相关文章

  1. 面试常问的几个排序和查找算法,PHP实现

    冒泡,快排,二分查找,都是面试常问的几个算法题目,虽然简单,但是一段时间不用的话就很容易忘记,这里我用PHP实现了一下,温故而知新. 排序 冒泡排序 每一次冒出一个最大的值 function bubb ...

  2. python基础之面试常问

    目录 python相对其他语言有什么特点? python内存管理机制,gc机制的了解,gc回收三种算法. lambda函数 高级函数 map.reduce.filter.sorted等. 简述六种基本 ...

  3. 面试常问的dubbo的spi机制到底是什么?

    前言 dubbo是一款微服务开发框架,它提供了 RPC通信 与 微服务治理 两大关键能力.作为spring cloud alibaba体系中重要的一部分,随着spring cloud alibaba在 ...

  4. 各大互联网公司java开发面试常问问题

    本人是做java开发的,这是我参加58,搜狐,搜狗,新浪微博,百度,腾讯文学,网易以及其他一些小的创业型公司的面试常被问的问题,当然有重复,弄清楚这些,相信面试会轻松许多. 1. junit用法,be ...

  5. Python面试常问的10个问题

    很多人想找Python工作,面试往往在基础知识点采坑了 Python是个非常受欢迎的编程语言,随着近些年机器学习.云计算等技术的发展,Python的职位需求越来越高.下面我收集了10个Python面试 ...

  6. 面试常问Spring IOC,不得不会。

    广义的 IOC IoC(Inversion of Control) 控制反转,即“不用打电话过来,我们会打给你”. 两种实现: 依赖查找(DL)和依赖注入(DI). IOC 和 DI .DL 的关系( ...

  7. Java面试常问的问题(转载)

    并发.JVM.分布式.TCP/IP协议 1)Java的数据结构相关的类实现原理,比如LinkedList,ArrayList,HashMap,TreeMap这一类的.以下简单模拟一个数据结构的连环炮. ...

  8. Android面试常问到的知识点

    一.算法,数据结构 1.排序算法 2.查找算法 3.二叉树 4.广度,深度算法: 二.java基础 1.集合Collection,List,Map等常用方法,特点,关系: 2.线程的同步,中断方式有几 ...

  9. 面试常问的 25+ 个 Linux 命令

    作为一个Java开发人员,有些常用的Linux命令必须掌握.即时平时开发过程中不使用Linux(Unix)或者mac系统,也需要熟练掌握Linux命令.因为很多服务器上都是Linux系统.所以,要和服 ...

随机推荐

  1. ant design pro (四)新增页面

    一.概述 参看地址:https://pro.ant.design/docs/new-page-cn 这里的『页面』指配置了路由,能够通过链接直接访问的模块,要新建一个页面,通常只需要在脚手架的基础上进 ...

  2. element-ui 源码学习

    https://athena0304.github.io/element-analysis/ 1.模板字符串实现字符串拼接 typeClass() { return `el-alert--${ thi ...

  3. Openerp 中打开 URL 的三种 方法

    来自:http://shine-it.net/index.php/topic,8013.0.html 最近总结了,Openerp 中打开 URL 的三种 方法: 一.在form view 添加 < ...

  4. 浅析C++中的this指针

    转自:http://blog.csdn.net/starlee/article/details/2062586 有下面的一个简单的类: class CNullPointCall { public: s ...

  5. 从缓存行出发理解volatile变量、伪共享False sharing、disruptor

    volatilekeyword 当变量被某个线程A改动值之后.其他线程比方B若读取此变量的话,立马能够看到原来线程A改动后的值 注:普通变量与volatile变量的差别是volatile的特殊规则保证 ...

  6. java反射--方法反射的基本操作

    方法的反射 1)如何获取某个方法 方法的名称和方法的参数列表才能唯一决定某个方法. 2)方法反射的操作 method.invoke(对象,参数列表). 代码实例: package com.reflec ...

  7. PHP-汇总CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI

    什么是CGI 1.CGI是HTTP协议与其他外部应用程序之间的一个接口标准 2.CGI程序或脚本(CGI程序通过HTTP服务器去执行时, 必须在CGI程序中制定其执行程序的完整路径, 使SHELL能找 ...

  8. Ubuntu下设置redis让其他服务器访问

    修改redis配置文件,将 bind 127.0.0.1to bind 0.0.0.0Then restart your service (service redis-server restart) ...

  9. Linux-软件包管理-yum在线管理-yum命令

    yum list 查看所有可用软件包列表 vim /etc/yum.repos.d/CentOS-Base.repo 查看当前linux系统默认的网络yum源信息 yum search httpd 搜 ...

  10. HTTP协议-状态码详解(转)

    原文:http://www.cnblogs.com/lebronjames/archive/2013/01/10/2854981.html HTTP状态码的学习资料到处都有,但是都是理论上讲解.  本 ...