What is the difference between an integer and an int in Java __java

Source: Internet
Author: User
Tags wrapper

Java is an object-oriented programming language, everything is object, but for the convenience of programming or the introduction of basic data types, in order to be able to use these basic data types as objects, Java for each of the basic data types introduced a corresponding wrapper type (wrapper class), int's wrapper class is integer, starting with Java 5, the automatic boxing/unboxing mechanism is introduced, allowing the two to convert to each other as follows:

Original type: boolean,char,byte,short,int,long,float,double

Packing type: boolean,character,byte,short,integer,long,float,double

Incidentally, there are only 8 basic data types in Java, except for the base type (primitive type), and the rest are reference types (reference type).

so the basic difference is: Ingeter is the wrapper class for int, and the initial value of an int is 0,ingeter null. In addition to the difference, please see the code:

public class Testinteger {public
    static void Main (string[] args) {
        int i = 128;
        Integer i2 = 128;
        Integer i3 = new Integer (128);
        System.out.println (i = = i2); Integer automatically unboxing as int, so it is true
        System.out.println (i = = i3)//true, the reason for the previous
        integer i4 = 127;//compile-time translated to: integer I4 = Integer.valueof (127);
        Integer i5 = 127;
        System.out.println (i4 = = i5);//true
        Integer I6 = 128;
        Integer i7 = 128;
        System.out.println (I6 = = i7);//false
        integer i8 = new Integer (127)
        ; SYSTEM.OUT.PRINTLN (i5 = = i8); False
        integer I9 = new Integer (128);
        Integer i10 = new Integer (123);
        System.out.println (I9 = = i10);  False
    }
}

Why I4 and i5 ratios are true, and I6 and i7 ratios are false. The key is to see the valueof () function, this function for the number between 128 to 127, will be cached, Integer i5 = 127, will be 127 cache, the next time to write an Integer I6 = 127, will be directly from the cache, it will not be new. So the I4 and i5 ratios are true, and the I6 and i7 ratios are false.

For the i5 and i8 behind, and I9 and i10, this is false because the objects are different.

the above summary is summarized as follows:

1, at any rate, the integer and the new integer are not equal. Instead of going through the unboxing process, new objects are stored in the heap, and not new integer constants are Chang (in the method area) and their memory addresses are different, so false.

2, two are not new integers, if the number is between 128 and 127, it is true, otherwise false. Because Java compiles integer i2 = 128, it is translated as an integer i2 = integer.valueof (128), while the valueof () function caches the number between 128 and 127.

3, two are new, all false. Or the memory address is not the same.

The 4,int and integer (regardless of the new No) ratio are true because the integer can be automatically disassembled to int.


Reference from: Http://www.cnblogs.com/liuling/archive/2013/05/05/intAndInteger.html and http://blog.csdn.net/jackfrued/ article/details/44921941

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.