Home  >  Article  >  Java  >  What is the difference between int and integer in java?

What is the difference between int and integer in java?

青灯夜游
青灯夜游Original
2019-12-26 16:26:463968browse

The difference between int and integer in java

●Int is the basic data type, and int variables store numerical values. ;Integer is a reference data type, which is actually an object. Integer stores the address of the reference object .

●The default value of int is 0, and the default value of Integer is null;

●The int type directly stores the value, and the Integer needs to instantiate the object and point to the address of the object.

[Recommended learning: Java video tutorial]

Comparison of the memory occupied by int and Integer:

Integer objects will occupy More memory. Integer is an object and needs to store the metadata of the object. But int is a primitive type of data, so it takes up less space.

The Integer variable generated by non-new is compared with the variable generated by new Integer(), and the result is false.

/**
 * 比较非new生成的Integer变量与new生成的Integer变量
 */public class Test {
    public static void main(String[] args) {
        Integer i= new Integer(200);
        Integer j = 200;
        System.out.print(i == j);        //输出:false
    }
}

Because the Integer variable generated by non-new points to an object in the Java constant pool, while the variable generated by new Integer() points to a newly created object in the heap, the addresses in the memory of the two are different. . So the output is false.

Compare two non-new generated Integer objects

If the values ​​of the two variables are between the interval [-128,127], the comparison result is true; otherwise, The result is false.

/**
 * 比较两个非new生成的Integer变量
 */public class Test {
    public static void main(String[] args) {
        Integer i1 = 127;
        Integer j1 = 127;
        System.out.println(i1 == j1);//输出:true
        Integer i2 = 128;
        Integer j2 = 128;
        System.out.println(i2 == j2);//输出:false
    }
}

When java compiles Integer i1 = 127, it will be translated into Integer i1 = Integer.valueOf(127).

Integer variable (whether generated by new or not) is compared with int variable

As long as the values ​​of the two variables are equal, the result is true.

/**
 * 比较Integer变量与int变量
 */public class Test {
    public static void main(String[] args) {
        Integer i1 = 200;
        Integer i2 = new Integer(200);        int j = 200;
        System.out.println(i1 == j);//输出:true
        System.out.println(i2 == j);//输出:true
    }
}

When the wrapped Integer variable is compared with the basic data type int variable, the Integer will be automatically unpacked into an int and then compared. In fact, the two int variables are compared. The values ​​are equal, so they are true.

The above is the detailed content of What is the difference between int and integer in java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn