好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java 如何判断Integer类型的值是否相等

判断Integer类型的值是否相等

我们知道Integer是int的包装类,在jdk1.5以上,可以实现自动装箱拆箱,就是jdk里面会自动帮我们转换,不需要我们手动去强转,所以我们经常在这两种类型中随意写,平时也没什么注意 但Integer他是对象,我们知道 == 比较的是堆中的地址,但有个奇怪的事是, 如果 Integer a = 123, Integer b = 123,可以返回true,但如果Integer a = 12345, Integer b = 12345,返回false

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

public class Demo {

     public static void main(String[] args) {

         Integer c = - 128 ;

         Integer d = - 128 ;

         System.out.println( "c == d: " + (c == d));

         System.out.println( "c.equals(d): " + c.equals(d));

         System.out.println( "c.intValue() == d.intValue(): " + (c.intValue() == d.intValue()));

         System.out.println( "Objects.equals(c, d): " + Objects.equals(c, d));

        

         Integer e = 127 ;

         Integer f = 127 ;

         System.out.println( "e == f: " + (e == f));

         System.out.println( "e.equals(f): " + e.equals(f));

         System.out.println( "e.intValue() == f.intValue(): " + (e.intValue() == f.intValue()));

         System.out.println( "Objects.equals(e, f): " + Objects.equals(e, f));

        

         Integer g = 128 ;

         Integer h = 128 ;

         System.out.println( "g == h: " + (g == h));

         System.out.println( "g.equals(h): " + g.equals(h));

         System.out.println( "g.intValue() == h.intValue():" + (g.intValue() == h.intValue()));

         System.out.println( "Objects.equals(g, h): " + Objects.equals(g, h));

     }

}

结果如下:

c == d: true
c.equals(d): true
c.intValue() == d.intValue(): true
Objects.equals(c, d): true
e == f: true
e.equals(f): true
e.intValue() == f.intValue(): true
Objects.equals(e, f): true
g == h: false
g.equals(h): true
g.intValue() == h.intValue():true
Objects.equals(g, h): true

(1)当用[==]进行比较时,jvm默认是比较数据在java堆的地址。int是一种基本数据类型,jvm会自动将Integer转成int数值进行比较。在Integer类中,有一个内部静态类IntegerCache ,用来支持自动拆箱和装箱,如下,数值范围[-128,127]

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

/**

      * Cache to support the object identity semantics of autoboxing for values between

      * -128 and 127 (inclusive) as required by JLS.

      *

      * The cache is initialized on first usage.  The size of the cache

      * may be controlled by the -XX:AutoBoxCacheMax=<size> option.

      * During VM initialization, java.lang.Integer.IntegerCache.high property

      * may be set and saved in the private system properties in the

      * sun.misc.VM class.

      */

 

     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() {}

     }

默认IntegerCache.low 是-127,Integer.high是128,如果在这个区间[-128,127]内,他就会把变量i当做一个变量,放到内存中,用[==]比较是会得出true;但如果不在这个范围内,就会去new一个Integer对象,当运用[==]时,会比较Integer两个对象地址,得出false。

比较两个Integer的值是否相同,方法比较多:

1、推荐用equals(),这个还可以避免一些空指针问题的出现。

2、或者使用Integer.intValue();这样出来的就是int值,就可以直接比较了(可能会抛出空指针异常);

Integer赋值比较

Integer是int的包装类,继承Number类另实现Comparable接口,其取值范围为:-2147483648~2147483647

赋值操作

?

1

2

3

4

5

6

7

8

9

10

11

12

13

Integer newInt = new Integer( 10 );

//自动装箱操作,编译后class文件中为:Integer num = Integer.valueOf(10);

Integer num = 10 ;

//num.intValue()为解包操作

int intNum = num.intValue();

/**

  * 输出结果为true----false----true,原因:

  * 1、 Integer为引用类型,引用类型栈中变量表示一个地址指向堆中的一片内存空间

  * 2、 newInt.equals(num)为true是因为Integer重写了equals方法,equals方法中实际是值比较

  * 3、(newInt == num)为false因为两个变量不指向同一片内存

  * 4、(intNum==num)结果为true是因为intNum是基本数据类型,值直接存在栈中,两者比较的是值

  */

System.out.println(newInt.equals(num)+ "----" +(newInt == num)+ "----" +(intNum==num));

?

1

2

3

4

5

6

7

8

private final int value;

public boolean equals(Object obj) {

     if (obj instanceof Integer) {

      //将对象强制转换为Integer类型,然后自动拆箱进行==比较

         return value == ((Integer)obj).intValue();

     }

     return false ;

}

构造函数

?

1

2

3

4

5

6

7

8

public Integer( int value) {

  //参数为int 直接赋值

  this .value = value;

}

public Integer(String s) throws NumberFormatException {

  //参数为String的情况下调用paseInt()方法进行赋值,10表示为按10进制转换

  this .value = parseInt(s, 10 );

}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://HdhCmsTestcnblogs测试数据/ryelqy/p/11014766.html

查看更多关于Java 如何判断Integer类型的值是否相等的详细内容...

  阅读:26次