Integer对象的==比较

1.

Integer a = 127;
Integer b = 127;
System.out.println(a.hashCode()==b.hashCode());
System.out.println(a==b);
System.out.println(a.equals(b));

结果为:

true
true
true

2.

Integer a = 128;
Integer b = 128;
System.out.println(a.hashCode()==b.hashCode());
System.out.println(a==b);
System.out.println(a.equals(b));

结果为:

true
false
true

都是==比较,为什么一个true,一个false呢?

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

结论:

Integer的==比较,最终会走到Integer的valueOf方法。

public static Integer valueOf(int i) {
        if(i >= -128 && i <= IntegerCache.high)
            return IntegerCache.cache[i + 128];
        else
            return new Integer(i);
    }

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

其中IntegerCache.high =128

 

如果要比较的值,在-128—127之间,走的就是IntegerCache,那么比较结果==就是相等的

如果要比较的值,不在-128—127之间,走的就是 new Integer(i),==结果就不一样了。

附上 IntegerCache的源码:

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() {}
    }
时间: 2024-08-07 03:26:31

Integer对象的==比较的相关文章

JAVA中Integer对象的引用

JAVA中没有指针一说,但也有引用的概念.这里要说的主要是Integer是不是同一个对象. 1.先看一段代码: public static void main(String[] args){ Integer a1 = 100; Integer b1 = a1;//另一种也可以b1=100 Field field = null; try { field = a1.getClass().getDeclaredField("value"); } catch (NoSuchFieldExcep

Integer对象用== 和equals比较大小的问题

Integer对象用== 和equals比较大小 先看以下代码: Integer i1 = 127; Integer i2 = 127; if (i1 == i2) { System.out.println("i1 == i2 (==)"); } else { System.out.println("i1 != i2 (==)"); } Integer i3 = 128; Integer i4 = 128; if (i3 == i4) { System.out.pr

关于Integer对象比较,和int基本类型比较的一些问题

public class Test { public static void main(String[] args) { Integer j = 192; int i = 192; System.out.println(new Integer(12) == new Integer(12));//false对象比较.///必然不同 System.out.println(new Integer(192) == i);//true自动拆箱 System.out.println(j == i);//tr

两个值相同的Integer对象的比较

package com.jj.test; import java.util.Objects; public class ObjectEqualsTest { public static final Integer COUNT_NONE = 0; public static void main(String[] args){ Integer a=new Integer(0); boolean bool = Objects.equals(a,COUNT_NONE); a.equals(COUNT_N

Integer对象大小比较问题

一.问题 先来看一看例子 public class IntegerTest { public static void main(String[] args) throws Exception { Integer a1 = 127; Integer b1 = Integer.valueOf(127); System.out.println("1:"+(a1 == b1)); Integer a2 = 127; Integer b2 = 127; System.out.println(&q

再谈包装类Integer对象的比较

public class CompareDemo { public static void main(String[] args) { int a = 128, b = 128; System.out.println(a == b); // true Integer c = 128, d = 128; System.out.println(c == d); // false System.out.println(c.equals(d)); // true Integer e = -128, f

int类型与Integer对象转换,其他基本类型转换方式相同。

int对应的是Integer,char对应的Character,其他6个都是基本类型首字母大写. parseXXX(String s);其中XXX表示基本类型,参数为可以转成基本类型的字符串. System.out.println(Integer.parseInt("123") + 2); //打印结果为 125 //字符串->基本数据类型        String str="12";//字符串,必须时整型字符串        int num=Integer

jsp中遇到Integer的方法valueOf()和parseInt()的区别.前者要求是对象类型,后者是数字型字符串

他们有本质区别,Integer.valueof(String s)是将一个包装类是将一个实际值为数字的变量先转成string型再将它转成Integer型的包装类对象(相当于转成了int的对象)这样转完的对象就具有方法和属性了. 而Integer.parseInt(String s)只是将是数字的字符串转成数字,注意他返回的是int型变量不具备方法和属性 Integer.parseInt()把String 型转换为Int型, Integer.valueOf()把String 型转换为Integer

搜狗一道java题目 关于对象 synchronized 关键字作用在 int, integer

第一次见到这个题目,我觉得自己没学到java,太浅了,其实这个问题没有考synchronized关键字,只是考什么是对象? 1.在java编程思想的第二章有一句话; 一切都是对象,很可惜int,char 等不是的,虽然他们有他们的包装类,但是java还是保留了int 这些好用的原子类型, 对于synchronized后面可以跟对象和.class所以 字符串是对象可以,Integer对象可以,ExampleCLass.class可以. 我想问的是,数组是对象吗? 查了一下,是对象,所以数组也是答案