The difference between Java Learning _int and integer

Source: Internet
Author: User

Data types in 1.Java are divided into basic data types and reference data types

int is the basic data type, and integer is the reference data type;

Ingeter is the wrapper class for int, and the initial value of int is null for 0,ingeter.

2. Initialization

int i = 1;

Integer i= new Integer (1);

With automatic boxing and unpacking, the integer class can also be used: integer i= 1;

3. Automatic box packing and unpacking

3.1 Auto-Boxing: Encapsulates the base data type as an object type, making it possible to invoke all the methods declared by an object later.

1 // declares an integer object 2 Integer num = ten; 3 // The above statement is the use of automatic boxing: resolved to 4 New Integer (10);

3.2 Auto-unpacking: Converts an object to a basic data type.

1 // Packing 2 Integer num = ten; 3 // Unpacking 4 int num1 = num;

A typical use of automatic unpacking is when the operation is performed: Because the object is not directly operational, it is converted to the basic data type before it can be subtraction.

Integer num = ten; // automatic unpacking in the case of calculations System.out.print (num--);

Advanced:

// The number outside the -128~127 Integer num1 = 297;   Integer num2 = 297;           System.out.println ("num1==num2:" + (num1==num2));                     //  Integer num3 =;   Integer num4 =;   System.out.println (

The result of the printing is:

NUM1==NUM2: false

NUM3==NUM4:True

It's strange: this is due to the Java design of the automatic boxing and unpacking of integers and int, which is a pattern: the mode of sharing (flyweight)

In order to increase the reuse of simple numbers, Java defines: when auto-boxing for values from –128 to 127, they are boxed as an integer object, there is always only one object in memory that is reused.

If the value from –128 to 127 is exceeded, the boxed integer object is not reused, which is equivalent to creating an integer object each time it is boxed;

The above phenomenon is caused by the use of automatic boxing, if you do not use automatic boxing, but the same as the General class, with new to instantiate, will be a new object whenever new;

This automatic boxing unboxing is not only used in the basic data types, but also in the string class, such as when we declare a string object often:

1 String str = "SL"; 2 // instead of the following declaration method 3 New String ("SL");

reprinted from: Liang Tsai's blog

The difference between Java Learning _int and integer

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.