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

What is the difference between int and integer in java?

Dec 26, 2019 pm 04:26 PM
int integer java

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
    }
}
Copy after login

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
    }
}
Copy after login

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
    }
}
Copy after login

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 of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Square Root in Java

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Perfect Number in Java

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Random Number Generator in Java

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Armstrong Number in Java

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Weka in Java

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Smith Number in Java

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

Java Spring Interview Questions

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Break or return from Java 8 stream forEach?

See all articles