Silly? Integer, new Integer () and int interview questions

Author: chenxiangxiang https://www.cnblogs.com/cxxjohnson/p/10504840.html

This is interesting: The magical use and trap of IntegerCache!

Distinction of basic concepts:

1. Integer is a wrapper class for int, int is a basic data type of java
2. Integer variables must be instantiated before they can be used, and int variables do not need to be
3. Integer is actually a reference to an object. In fact, a pointer is generated to point to this object; and int directly stores the data value
4. The default value of Integer is null, and the default value of int is 0

Integer, new Integer() And  int comparison

  • 1. new Integer() Comparison of two  variables, always false

Because new generates two objects, their memory addresses are different

Integer i = new Integer(100);
Integer j = new Integer(100);
System.out.print(i == j);  //false
  • 2. IntegerVariable and  new Integer() variable comparison, always false.

Because the Integer variable points to the object in the java constant pool,
and the variable of new Integer () points to the newly created object in the heap, the two have different addresses in memory.

Integer i = new Integer(100);
Integer j = 100;
System.out.print(i == j);  //false
  • 3. Two Integer variables are compared. If the values ​​of the two variables are in the interval -128 to 127, the comparison result is true. If the values ​​of the two variables are not in this interval, the comparison result is false.
Integer i = 100;
Integer j = 100;
System.out.print(i == j); //trueInteger i = 128;
Integer j = 128;
System.out.print(i == j); //false

Analysis:
Integer i = 100 At compile time, it will be translated into  Integer i = Integer.valueOf(100), and java defines the valueOf of Integer type as follows:

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

Java caches numbers between -128 and 127.
Therefore, when Integer i = 127, 127 will be cached. Next time when Integer j = 127 is written, it will be taken directly from the cache and will not be new.

  • 4,  int variable  Integernew Integer() when compared, as long as the two values are equal, then true

Because when the packaging class Integer and the basic data type int are compared, java will automatically unpack it as int, and then compare, in fact it becomes a comparison of two int variables.

Integer i = new Integer(100); //自动拆箱为 int i=100; 此时,相当于两个int的比较int j = 100;
System.out.print(i == j); //true

Example 1:

public class IntegerDemo {    public static void main(String\[\] args) {        int i = 128;
        Integer i2 = 128;
        Integer i3 = new Integer(128);

        System.out.println("i == i2 = " + (i == i2)); // Integer会自动拆箱为int,所以为true
        System.out.println("i == i3 = " + (i == i3)); // true,理由同上

        Integer i4 = 127;// 编译时被翻译成:Integer i4 = Integer.valueOf(127);
        Integer i5 = 127;
        System.out.println("i4 == i5 = " + (i4 == i5));// true

        Integer i6 = 128;
        Integer i7 = 128;
        System.out.println("i6 == i7 = " + (i6 == i7));// false

        Integer i8 = new Integer(127);
        System.out.println("i5 == i8 = " + (i5 == i8)); // false

        Integer i9 = new Integer(128);
        Integer i10 = new Integer(128);
        System.out.println("i9 == i10 = " + (i9 == i10)); // false
    }}

the answer is

i == i2 = truei == i3 = truei4 == i5 = truei6 == i7 = falsei5 == i8 = falsei9 == i10 = false

Example 2:

package demo1.demo1;public class Campare {    public static void main(String\[\] args) {

        Integer a = new Integer(127), b = new Integer(128);        int c = 127, d = 128, dd = 128;
        Integer e = 127, ee = 127, f = 128, ff = 128;

        System.out.println(a == b); // false 因为a,b都是new出来的对象,地址不同所以为false
        System.out.println(a == c); // true a会自动拆箱为int类型
        System.out.println(a == e); // false 指向的地址不同a是new出来的

        System.out.println(e == c); // true e会自动拆箱为int类型
        System.out.println(e == ee);// true Integer对 处于-128到127范围之间,指向了同一片地址区域

        System.out.println(b == f); // false 指向的地址不同b是new出来的
        System.out.println(f == d); // true f自动拆箱为int类型

        System.out.println(f == ff); /*
                                         * false 指向的不是同一片地址区域。
                                         * 在Integer类型中,-128到127存放的是同一片区域地址,
                                         * 之外的数是另外开辟空间来进行 存储的
                                         */
        System.out.println(d == dd); // true 不解释
    }}

Example 3:

Integer i01 = 59;int i02 = 59;
Integer i03 =Integer.valueOf(59);
Integer i04 = new Integer(59);

以下输出结果为false的是:

System.out.println(i01== i02);
System.out.println(i01== i03);
System.out.println(i03== i04);
System.out.println(i02== i04);

Analysis:
i01 == i02. i01.intValue () i02 Comparison of two values ​​5959-> true;

i01 == i03. Since 59 is between -128 and 127, the assignment operations of i01 and i03 return the same object. Both are the same object returned from chche, the object address is the same true;

i03 == i04. i03 is from the cache value, i04 is the object of new, the two are not the same object, so false.

i02 == i04. Similar to the first one, true.

The answer is C.

Example 4:
The only difference from Example 3 is that the values ​​are all changed to 128.

Integer i01 = 128;int i02 = 128;
Integer i03 = Integer.valueOf(128);
Integer i04 = new Integer(128);

以下输出结果为false的是:
System.out.println(i01 == i02);
System.out.println(i01 == i03);
System.out.println(i03 == i04);
System.out.println(i02 == i04);

answer:

truefalsefalsetrue

I recommend going to my blog to read more:

1. Java JVM, collection, multithreading, new features series tutorials

2. Spring MVC, Spring Boot, Spring Cloud series of tutorials

3. Maven, Git, Eclipse, Intellij IDEA series of tool tutorials

4. The latest interview questions for Java, backend, architecture, Alibaba and other major manufacturers

Life is beautiful, see you tomorrow ~

495 original articles have been published · 1032 thumbs up · 1.46 million views

Guess you like

Origin blog.csdn.net/youanyyou/article/details/105558678