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

Source: Internet
Author: User

Java is an object-oriented programming language, everything is an object, but for the convenience of programming or introduce basic data types, in order to be able to use these basic data types as Object operations, Java for each basic data type has introduced the corresponding wrapper type (wrapper class), int's wrapper class is integer, starting with Java 5 to introduce the automatic boxing/unpacking mechanism, so that the two can be converted to each other, corresponding to the following:

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

Package Type: boolean,character,byte,short,integer,long,float,double

By the way, the basic data type in Java is only 8 of the above, except for the base type (primitive type), and all that is left is the reference type (reference type).

So the fundamental difference is that Ingeter is the wrapper class for int, and the initial value of int is null for the initial value of 0,ingeter. In addition to the difference, see the code:

public class Testinteger {public      static void Main (string[] args) {          int i = n;          Integer i2 = n;          Integer i3 = new Integer (+);          System.out.println (i = = i2); The integer is automatically unboxing to int, so true          System.out.println (i = = i3);//true, for the same reason that          integer i4 = 127;//was translated when compiled: integer i4 = Integer.valueof (127);          Integer i5 = 127;          System.out.println (i4 = = i5);//true          Integer I6 =;          Integer i7 = n;          System.out.println (I6 = = i7);//false          integer i8 = new Integer (127);          SYSTEM.OUT.PRINTLN (i5 = = i8); False          integer I9 = new Integer (+);          Integer i10 = new Integer (123);          System.out.println (I9 = = i10);  False      }  }  

  

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

For the i5 and i8 behind, as well as I9 and i10, because the objects are different, so false.

The above situation is summarized as follows:

1, in any case, the integer is not equal to the new integer. Do not experience the unboxing process, the new object is stored in the heap, but not the new integer constant in the Chang (in the method area), their memory address is not the same, so false.

2, two are non-new integer, if the number is between 128 to 127 is true, otherwise false. Since Java is compiled with integer i2 = 128, it is translated into: integer i2 = integer.valueof (128), and the valueOf () function caches the number between 128 and 127.

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

4,int and Integer (regardless of new) are true because the integer is automatically removed as an int.

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

Related Article

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.