Packing and unpacking in Java

Source: Internet
Author: User

After J2SE5.0, the automatic packing and unpacking functions were introduced to improve our development efficiency. However, automatic packing and unpacking are actually supported by the compiler (not the language itself, or virtual machine ), therefore, this kind of support also hides some internal entities, and adds some Class Optimization (for example, the cache in Integer, refer to the cache section ), it is easier to create problems in a specific environment, and if you do not know that the original debugging is not available. The following briefly introduces the implementation of the compiler for packing and unpacking, and briefly introduces several problems that may occur according to the implementation.

Packing and unpacking
The following packing and unpacking code:


Object value = 10;
Int intValue = (Integer) value;
Integer newIntValue = new Integer (10 );
 

The compiled bytecode is as follows:

0 bipush 10

2 invokestatic java. lang. Integer. valueOf (int): java. lang. Integer [20]

5 astore_1 [value]

6 aload_1 [value]

7 checkcast java. lang. Integer [21]

10 invokevirtual java. lang. Integer. intValue (): int [26]

13 istore_2 [intValue]

14 new java. lang. Integer [21]

17 dup

18 bipush 10

20 invokespecial java. lang. Integer (int) [30]

23 astore_3 [newIntValue]

From the bytecode above, we can see that 10 calls the valueOf method to convert the value to an Integer instance first, and then assigns the value. After the value is forcibly converted to an Integer class, the intValue method is called and then assigned to intValue. This is to use the compiler to implement packing and unpacking.

 

Strange NullPointerException
Check the following code:


Integer value = null;
Int intValue = value;
 

It can be compiled, but NullPointerException occurs during running. What caused this? Take a look at the bytecode:

0 aconst_null

1 astore_1 [value]

2 aload_1 [value]

3 invokevirtual java. lang. Integer. intValue (): int [20]

6 istore_2 [intValue]

From the bytecode, we can see that the intValue function is called directly on the value instance when the value is assigned.

For the current code, we can see at a glance that the current value is null, but what if this null is assigned far away? Or indirect value assignment? At this time, this problem will be quite strange.

 

Equality and inequality
Check the Code:


Integer value1 = 100;
Integer value2 = 100;
System. out. println ("value1 = value2 is" + (value1 = value2 ));

Integer valuee3 = 200;
Integer valuee4 = 200;
System. out. println ("value3 = value4 is" + (value3 = value4 ));
 

What is the result of this code?

Value1 = value2 is true

Value3 = value4 is false

 

The two pieces of code have different values. What are the differences between other codes? This is because the valueOf method is called during the packing process, and the valueOf method caches the value between-128 and 127 (see the cache section ), therefore, the reference of value1 and value2 is the same, while the reference of value3 and value4 is different, and = is the reference, so the above results will appear.

The actual practice should be:

Integer value1 = 100;
Integer value2 = 100;
System. out. println ("value1 = value2 is" + (value1.equals (value2 )));

Integer valuee3 = 200;
Integer valuee4 = 200;

System. out. println ("value3 = value4 is" + (value3.equals (value4 )));

The expected result is as follows:

Value1 = value2 is true

Value3 = value4 is true

 

Therefore, we must use the "=" operator with caution.

 

Equality and inequality in String
Similar cases are also found in String. Check the Code:


String str1 = "abc ";
String str2 = "abc ";
System. out. println ("str1 = str2 is" + (str1 = str2 ));

String str3 = new String ("abc ");
String str4 = new String ("abc ");
System. out. println ("str3 = str4 is" + (str3 = str4 ));
 

Execution result:

Str1 = str2 is true

Str3 = str4 is false

 

This is because str1 and str2 use the same string, that is, the string in the character constant, str3 and str4 create two new string objects by using the character in the character constant as the parameter, so they are not equal in the case of reference comparison. We can get the information from the bytecode (delete the printed code ):

0 ldc <String "abc"> [20]

2 astore_1 [str1]

3 ldc <String "abc"> [20]

5 astore_2 [str2]

6 new java. lang. String [22]

9 dup

10 ldc <String "abc"> [20]

12 invokespecial java. lang. String (java. lang. String) [24]

15 astore_3 [str3]

16 new java. lang. String [22]

19 dup

20 ldc <String "abc"> [20]

22 invokespecial java. lang. String (java. lang. String) [24]

25 astore 4 [str4]

The correct method is to call the equals method, instead of using the "=" operator.

 

About Cache
According to the current information, cache classes include Byte, Short, Integer, Long, and Boolean. This cache only exists when the valueOf (static) method is called (the valueOf method is called for packing ). For integer types, the cached values are between-128 and 127 (including-128 and 127). Other values are not cached, but only true and false values are available for Boolean types. The Code is as follows:


Public final class Integer extends Number {

Public static Integer valueOf (int I ){
If (I >=- 128 & I <= IntegerCache. high)
Return IntegerCache. cache [I + 128];
Else
Return new Integer (I );
}
Public final class Boolean {

Public static Boolean valueOf (boolean B ){
Return (B? TRUE: FALSE );
}

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.