Java automatic packing and unpacking implementation method

Source: Internet
Author: User
Tags unpack

Java automatic packing and unpacking implementation method

What is automatic packing and unpacking?

Autoboxing and unboxing are functions provided from j2se 5.0.

Generally, when we want to create a class object, we will:

Class a = new class (parameter );

When we create an integer object, we can:

Integer I = 100; (note: not int I = 100 ;)

In fact, the above code is equivalent to: integer I = new integer (100); this is the automatic packing function of the basic data type.

 

?? Differences between basic data types and objects

The basic data type is not an object, that is, variables and constants defined by int, double, and boolean.

There is no callable method for the basic data type.

Eg: int t = 1; t. There is no method drop behind it.

Integer t = 1; t. There are many methods that you can call later.

 

?? When automatic packing

Example: integer I = 100;

The compiler automatically compiles the following syntax for you: integer I = new integer (100 );

 

?? When to automatically unpack

The unboxing function automatically removes basic data from an object. You can achieve automatic unpacking as follows:

1 integer I = 10; // boxed
2 int t = I; // unpack

 

During calculation, you can also perform automatic packing and unpacking.

1 integer I = 10;
2 system. out. println (I ++ );
Automatic integer packing

// In-128 ~ Number not greater than 127
Integer i1 = 200;
Integer i2 = 200;
System. out. println ("i1 = i2:" + (i1 = i2 ));
// In-128 ~ Number within 127
Integer i3 = 100;
Integer i4 = 100;
System. out. println ("i3 = i4:" + (i3 = i4 ));


The output result is:

I1 = i2: false
I3 = i4: true


Note:

Equals () compares whether the values (content) of two objects are the same.

"=" Compares the two objects with the same reference (memory address) and compares the variable values of the two basic data types.

 

When auto-packing is performed for values ranging from-128 to 127, they are packed as integer objects and will be reused in memory,

In this example, i3 and i4 actually refer to the same object.

If the value range from-128 to 127 is exceeded, the boxed integer object will not be reused,

That is, it is equivalent to creating an integer object during each packing. In this example, i1 and i2 refer to different objects.

 

?? Binning string

Let's take a look at an example:

1 string str1 = "abc ";
2 string str2 = "abc ";
3 system. out. println (str2 = str1); // The output is true.
4 system. out. println (str2.equals (str1); // The output is true.
5            
6 string str3 = new string ("abc ");
7 string str4 = new string ("abc ");
8 system. out. println (str3 = str4); // The output is false.
9 system. out. println (str3.equals (str4); // The output is true.

 


How can this be explained? It seems that nothing can be seen. Let's look at another example.

1 string d = "2 ";
2 string e = "23 ";
3 e = e. substring (0, 1 );
4 system. out. println (e. equals (d); // The output is true.
5 system. out. println (e = d); // The output is false.

 


In the second example, the initial values of e are different from those of d. Therefore, e and d create an object respectively, and (e = d) is false.

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.