首页 > 其他分享 >为什么Integer用==比较时127相等而128不相等?

为什么Integer用==比较时127相等而128不相等?

时间:2023-03-26 09:23:06浏览次数:43  
标签:相等 int IntegerCache 127 128 Integer 装箱

首先回顾一下自动装箱。对于下面这行代码:

Integer a = 1;

变量a为Integer类型,而1为int类型,且Integer和int之间并无继承关系,按照Java的一般处理方法,这行代码应该报错。

但因为自动装箱机制的存在,在为Integer类型的变量赋int类型值时,Java会自动将int类型转换为Integer类型,即:

Integer a = Integer.valueOf(1);

valueOf()方法返回一个Integer类型值,并将其赋值给变量a。这就是int的自动装箱。

自动装箱并不一定new出新的对象。

自动装箱涉及到的方法是Integer.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) {
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}

其注释里就直接说明了-128到127之间的值都是直接从缓存中取出的。看看是怎么实现的:如果int型参数i在IntegerCache.low和IntegerCache.high范围内,则直接由IntegerCache返回;否则new一个新的对象返回。似乎IntegerCache.low就是-128,IntegerCache.high就是127了。 

看看IntegerCache的源码:在其static块中就一次性生成了-128到127直接的Integer类型变量存储在cache[]中,对于-128到127之间的int类型,返回的都是同一个Integer类型对象。

整个工作过程就是:Integer.class在装载(Java虚拟机启动)时,其内部类型IntegerCache的static块即开始执行,实例化并暂存数值在-128到127之间的Integer类型对象。当自动装箱int型值在-128到127之间时,即直接返回IntegerCache中暂存的Integer类型对象。

为什么Java这么设计?我想是出于效率考虑,因为自动装箱经常遇到,尤其是小数值的自动装箱;而如果每次自动装箱都触发new,在堆中分配内存,就显得太慢了;所以不如预先将那些常用的值提前生成好,自动装箱时直接拿出来返回。哪些值是常用的?就是-128到127了。

不仅int,Java中的另外7中基本类型都可以自动装箱和自动拆箱,其中也有用到缓存。见下表:

解决方法

既然我们的目的是比较数值是否相等,而非判断是否为同一对象;而自动装箱又不能保证同一数值的Integer一定是同一对象或一定不是同一对象,那么就不要用==,直接用equals()好了。实际上,Integer重写了equals()方法,直接比较对象的数值是否相等。

 

标签:相等,int,IntegerCache,127,128,Integer,装箱
From: https://www.cnblogs.com/xfeiyun/p/17256210.html

相关文章

  • 「解题报告」ARC127F ±AB
    首先容易想到\(m\)较大时答案为\(m+1\)。具体的,当\(m\gea+b-1\)时,从任意一个位置出发都可以进行操作,所以答案为\(m+1\)。当\(m\lea+b-2\)时,我们发现,对于......
  • COMP41280密码学
    COMP41280:FirstAssignmentUploadasinglePDFfilewithyouranswersthroughBrightspace.Onlyonesubmissionattemptisallowed,sopleasedouble-checkyour......
  • P6031 CF1278F Cards 加强版
    \(\text{Solution}\)推式子有答案为\[\begin{aligned}Ans&=\sum_{i=0}^ni^k\dbinomni(\frac1m)^i(1-\frac1m)^{n-i}\end{aligned}\]\(i\)的上限为\(n\),......
  • 128道高频k8s高频面试题
    1、简述ETCD及其特点?2、简述ETCD适应的场景?3、简述什么是Kubernetes?4、简述Kubernetes和Docker的关系?5、简述Kubernetes中什么是Minikube、Kubectl、Kubelet?6、......
  • 「解题报告」ARC128F Game against Robot
    好厉害的题。震撼到了。大部分参考Atcoder计数乱做-苹果蓝17。我的观察能力还是太差,一点条件都观察不出来,连\(p\)固定怎么做都不会。下面令\(n\gets\frac{n}{2......
  • Java基本类型和包装类什么情况下判断相等(“==“或“equals“)?
    Java中判断两个变量是否相等有两种方式:一种是利用==运算符,另一种是利用equals方法。如果变量是基本类型,那么使用==和使用equals会得到相同的结果,都是判断变量的值是否......
  • 「解题报告」ARC128E K Different Values
    我还是很菜啊。先考虑判定问题。考虑先找出一些显然的必要条件。记\(m=\suma_i\)。那么我们首先对\(m\)进行分块,每\(k\)个一块,设块数为\(p\),最后一个块的大小为......
  • 「解题报告」ARC128D Neq Neq
    我果然还是适合做这种简单数数,难的我根本不会做,哈哈!首先我们发现,如果有两个完全相同的相邻的数,那么这两个数中的每一个数都不能够被删除,那么这实际上把整个序列分成了若干......
  • Oracle ORA-12725 unmatched parentheses in regular expression
    OracleORA-12725unmatchedparenthesesinregularexpression简单来说就是正则表达式中的括号问题这种一般就可以锁定使用正则的函数,例如regexp_replace、regexp_lik......
  • 用了那么久你知道127.0.0.1和 localhost 的区别吗?
    localhost(本地主机)通常被认为是IP地址127.0.0.1的同义词。尽管它们在功能上是相同的,但localhost和127.0.0.1之间存在很大差异。一、解释localhost是一个别......