前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Integer 值判断相等

Integer 值判断相等

作者头像
Jacob丶
发布2020-08-05 17:54:39
发布2020-08-05 17:54:39
1.6K00
代码可运行
举报
文章被收录于专栏:JacobJacob
运行总次数:0
代码可运行

Integer 值判断相等

案例:

代码语言:javascript
代码运行次数:0
运行
复制
public class Test {
    public static void main(String[] args) {
        Integer a = 127;
        Integer b = 127;
        System.out.println("a == b :"+ (a == b));
        System.out.println("a.equals(b):"+a.equals(b));
        String x = "127";
        String y = "127";
        System.out.println("Integer.valueOf(x) == Integer.valueOf(y) :" + (Integer.valueOf(x) == Integer.valueOf(y)));
        System.out.println("Integer.valueOf(x).equals(Integer.valueOf(y)):"+Integer.valueOf(x).equals(Integer.valueOf(y)));
        System.out.println("====================================================================");
        Integer a1 = 128;
        Integer b1 = 128;
        System.out.println("a1 == b:"+(a1 == b1));
        System.out.println("a.equals(b):"+a.equals(b1));
        String x1 = "128";
        String y1 = "128";
        System.out.println("Integer.valueOf(x1) == Integer.valueOf(y1) :" + (Integer.valueOf(x1) == Integer.valueOf(y1)));
        System.out.println("Integer.valueOf(x1).equals(Integer.valueOf(y1)):"+Integer.valueOf(x1).equals(Integer.valueOf(y1)));

    }
}

日志打印:

代码语言:javascript
代码运行次数:0
运行
复制
a == b :true
a.equals(b):true
Integer.valueOf(x) == Integer.valueOf(y) :true
Integer.valueOf(x).equals(Integer.valueOf(y)):true
====================================================================
a1 == b:false
a.equals(b):false
Integer.valueOf(x1) == Integer.valueOf(y1) :false
Integer.valueOf(x1).equals(Integer.valueOf(y1)):true

通过案例发现,值为127不管是 Integer 还是 String 类型,== 和 equals 都能比较成功。128与类型无关,与比较的方法有关。

equals

通过查看源码发现 Integer 重写的 equals、hashCode 方法,所以使用 equals 方法比较大小母庸质疑为 true.

代码语言:javascript
代码运行次数:0
运行
复制
    /**
     * Returns a hash code for this {@code Integer}.
     *
     * @return  a hash code value for this object, equal to the
     *          primitive {@code int} value represented by this
     *          {@code Integer} object.
     */
    @Override
    public int hashCode() {
        return Integer.hashCode(value);
    }

    /**
     * Returns a hash code for a {@code int} value; compatible with
     * {@code Integer.hashCode()}.
     *
     * @param value the value to hash
     * @since 1.8
     *
     * @return a hash code value for a {@code int} value.
     */
    public static int hashCode(int value) {
        return value;
    }

    /**
     * Compares this object to the specified object.  The result is
     * {@code true} if and only if the argument is not
     * {@code null} and is an {@code Integer} object that
     * contains the same {@code int} value as this object.
     *
     * @param   obj   the object to compare with.
     * @return  {@code true} if the objects are the same;
     *          {@code false} otherwise.
     */
    public boolean equals(Object obj) {
        if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false;
    }
==

127和127比较返回true,128和128比较返回false,有点出乎意料,主要是因为我们使用了惯用思维“以为/觉得”他们相等,没有经过认证。Integer a = 127; 是自动装箱会调用Interger.valueOf(int)方法;

代码语言:javascript
代码运行次数:0
运行
复制
    public static Integer valueOf(String s) throws NumberFormatException {
        return Integer.valueOf(parseInt(s, 10));
    }
	
	public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

默认 IntegerCache.low 是-127,Integer.high是128,如果在这个区间内,他就会把变量i当做一个变量,放到内存中;但如果不在这个范围内,就会去new一个 Integer 对象,所以使用 “==” 比较127返回true,比较128返回 false。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-04-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Integer 值判断相等
    • equals
    • ==
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档