The difference between int and integer in Java

Source: Internet
Author: User

1, int is the basic data type, the int variable stores the numeric value. An integer is a reference type, which is actually an object, and the integer stores the address of the referenced object.

2.

Integer i = new integer (100);
Integer j = new integer (100);
System. Out.print (i = = j);//false
Because new generates two objects with different memory addresses

3, int and integer accounted for memory comparison:

An integer object consumes more memory. An integer is an object that needs to store the object's metadata. But int is a primitive type of data, so it takes up less space.

4. The Integer variable generated by non-new is compared to the variable generated by the 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 a non-new integer variable points to an object in a Java constant pool, the new Integer () generates a variable that points to the new object in the heap, with a different address in memory. So the output is false.

5, two non-new integer objects are compared, if the value of two variables between the interval [-128,127], the 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 the integer i1 = 127, it translates to an integer i1 = integer.valueof (127).

6, the integer variable (whether or not new is generated) is compared to the int variable, so 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 }}

Wrapper class integer variables when compared to the base data type int variable, the integer is automatically wrapped to int and then compared, in fact, two int variables are compared, the values are equal, so true.

The difference between int and integer in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.