A small example of Java packaging class unpacking

Source: Internet
Author: User
Tags unpack

Simply put, packing is to convert the value type to the reference type, and unpacking is to convert the reference type to the value type.
In fact, there is nothing to say about this. You can see it in the Code:
Java code
/**
* @ Author hellosure
* @ Time 2011-7-27 8:10:46 a.m.
* @ Description: Example of packing and unpacking
*/
Public class Test {
 
Public static void main (String arg []) {
Int v1 = 100;
Int v2 = 100;
// Automatic Packing
Integer autovalue1 = 100;
Integer autovalue2= 100;
// Manual packing
Integer value1 = Integer. valueOf (v1 );
Integer value2 = Integer. valueOf (v2 );
Integer va1 = new Integer (v1 );
Integer va2 = new Integer (v2 );
// Automatically unpack
Int autov1 = autovalue1;
Int autov2 = autovalue2;
// Manually unpack
Int vv1 = value1.intValue ();
Int vv2 = value2.intValue ();


System. out. println ("v1 = v2 is" + (v1 = v2 ));
System. out. println ("autovalue1 = autovalue2 is" + (autovalue1 = autovalue2 ));
System. out. println ("value1 = value2 is" + (value1 = value2 ));
System. out. println ("va1 = va2 is" + (va1 = va2 ));
System. out. println ("va1 equals va2 is" + (va1.equals (va2 )));
System. out. println ("autov1 = autov2 is" + (autov1 = autov2 ));
System. out. println ("vv1 = vv2 is" + (vv1 = vv2 ));
 
System. out. println ("-----------------------------------------------------");

String strv1 = "100 ";
String strv2 = "100 ";
String stringv1 = new String ("100 ");
String stringv2 = new String ("100 ");
Integer strvalue1 = Integer. parseInt (strv1 );
Integer strvalue2 = Integer. parseInt (strv2 );
Integer stringvalue1 = Integer. parseInt (stringv1 );
Integer stringvalue2 = Integer. parseInt (stringv2 );
Integer newstrv1 = new Integer (strv1 );
Integer newstrv2 = new Integer (strv2 );
 
System. out. println ("strv1 = strv2 is" + (strv1 = strv2 ));
System. out. println ("stringv1 = stringv2 is" + (stringv1 = stringv2 ));
System. out. println ("stringv1 equals stringv2 is" + (stringv1.equals (stringv2 )));
System. out. println ("strvalue1 = strvalue2 is" + (strvalue1 = strvalue2 ));
System. out. println ("stringvalue1 = stringvalue2 is" + (stringvalue1 = stringvalue2 ));
System. out. println ("newstrv1 = newstrv2 is" + (newstrv1 = newstrv2 ));
System. out. println ("newstrv1 equals newstrv2 is" + (newstrv1.equals (newstrv2 )));

System. out. println ("-----------------------------------------------------");

Int v3 = 200;
Int v4 = 200;
// Automatic Packing
Integer autovalue3 = 200;
Integer autovalue4 = 200;
// Manual packing
Integer value3 = Integer. valueOf (v3 );
Integer value4 = Integer. valueOf (v4 );
Integer va3 = new Integer (v3 );
Integer va4 = new Integer (v4 );
// Automatically unpack
Int autov3 = autovalue3;
Int autov4 = autovalue4;
// Manually unpack
Int vv3 = value3.intValue ();
Int vv4 = value4.intValue ();


System. out. println ("v3 = v4 is" + (v3 = v4 ));
System. out. println ("autovalue3 = autovalue4 is" + (autovalue3 = autovalue4 ));
System. out. println ("value3 = value4 is" + (value3 = value4 ));
System. out. println ("va3 = va4 is" + (va3 = va4 ));
System. out. println ("va3 equals va4 is" + (va3.equals (va4 )));
System. out. println ("autov3 = autov4 is" + (autov3 = autov4 ));
System. out. println ("vv3 = vv4 is" + (vv3 = vv4 ));
 
System. out. println ("-----------------------------------------------------");

String strv3 = "200 ";
String strv4 = "200 ";
String stringv3 = new String ("200 ");
String stringv4 = new String ("200 ");
Integer strvalue3 = Integer. parseInt (strv3 );
Integer strvalue4 = Integer. parseInt (strv4 );
Integer stringvalue3 = Integer. parseInt (stringv3 );
Integer stringvalue4 = Integer. parseInt (stringv4 );
Integer newstrv3 = new Integer (strv3 );
Integer newstrv4 = new Integer (strv4 );
 
System. out. println ("strv3 = strv4 is" + (strv3 = strv4 ));
System. out. println ("stringv3 = stringv4 is" + (stringv3 = stringv4 ));
System. out. println ("stringv3 equals stringv4 is" + (stringv3.equals (stringv4 )));
System. out. println ("strvalue3 = strvalue4 is" + (strvalue3 = strvalue4 ));
System. out. println ("stringvalue3 = stringvalue4 is" + (stringvalue3 = stringvalue4 ));
System. out. println ("newstrv3 = newstrv4 is" + (newstrv3 = newstrv4 ));
System. out. println ("newstrv3 equals newstrv4 is" + (newstrv3.equals (newstrv4 )));

System. out. println ("-----------------------------------------------------");

}
}

Running result:
Java code
V1 = v2 is true
Autovalue1 = autovalue2 is true
Value1 = value2 is true
Va1 = va2 is false
Va1 equals va2 is true
Autov1 = autov2 is true
Vv1 = vv2 is true
----------------------------------------------------
Strv1 = strv2 is true
Stringv1 = stringv2 is false
Stringv1 equals stringv2 is true
Strvalue1 = strvalue2 is true
Stringvalue1 = stringvalue2 is true
Newstrv1 = newstrv2 is false
Newstrv1 equals newstrv2 is true
----------------------------------------------------
V3 = v4 is true
Autovalue3 = autovalue4 is false
Value3 = value4 is false
Va3 = va4 is false
Va3 equals va4 is true
Autov3 = autov4 is true
Vv3 = vv4 is true
----------------------------------------------------
Strv3 = strv4 is true
Stringv3 = stringv4 is false
Stringv3 equals stringv4 is true
Strvalue3 = strvalue4 is false
Stringvalue3 = stringvalue4 is false
Newstrv3 = newstrv4 is false
Newstrv3 equals newstrv4 is true
----------------------------------------------------

Summary:
For the two objects created by new, using = is certainly different, because it is not pointed to the same memory
The valueOf method is called during Integer packing, while the valueOf method caches values between-128 and 127. The source code is as follows:

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

It can be seen that the Integer cache contains a static Integer array. When the class is loaded, an Integer object ranging from-128 to 127 is created and saved in the cache array. Once the program calls the valueOf method, if the value of I is between-128 and 127, the Integer object is directly retrieved from the cache array.

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.