Automated unpacking and boxing in Java

Source: Internet
Author: User

Automatic Boxing and unpacking mechanism introduced in Java 1.5

Auto-Boxing: (See Code)

<span style= "Font-family:simsun;font-size:18px;color: #000000;" >//Automatic Boxing: Wrap the base types with their corresponding reference types so that they have the properties of the object integer integer_a = 8; </span>

Automatic unpacking: (see Code)

<span style= "Font-family:simsun;font-size:18px;color: #000000;" >//Automatic unpacking: <span style= "Font-family:simsun;font-size:12px;color: #000000;" > re-simplifies the object of a reference type such as Integer to the base type of data. </span></span>
<span style= "Font-family:simsun;font-size:18px;color: #000000;" >                 int a = new Integer (8);</span>

Note: Automatic boxing and unpacking is done by the compiler, and the compiler determines whether boxing and unboxing are performed at compile time based on syntax.

Java uses the automatic boxing and unpacking mechanism, which saves the memory overhead of common values and the overhead of creating objects, which improves efficiency.

(1) A variety of comparisons can be made between an integer and an int, and the integer object will automatically be compared with the int value after unpacking (see Code)

<span style= "Font-family:simsun;font-size:18px;color: #000000;" >                   Integer integer_a = 8;int a = 8; System.out.println (integer_a = = a);//TrueSystem.out.println (Integer_a > a);//FalseSystem.out.println (integer_a < a); Falseinteger Integer_b = 200;int B = 200; System.out.println (Integer_b = = b);//TrueSystem.out.println (Integer_b > B); FalseSystem.out.println (Integer_b < b); False</span>



(2) Two integer objects can also use >, < and other symbols compare size, two integer objects are removed, then compare size (see code)

<span style= "Font-family:simsun;font-size:18px;color: #000000;" >            Integer integer_c=8;      Integer integer_d=200;      System.out.println (integer_c>integer_d);//false   System.out.println (integer_c<integer_d);//true</ Span>


(3) Two integer objects should not be compared with = =. Because : the -128~127 range (which is typically this range) is used within an auto-boxing pool for objects within the cache , so it is equal to two different object reference comparisons outside the range, so they are unequal. (See Code)

<span style= "Font-family:simsun;font-size:18px;color: #000000;" >                  Integer integer_a_one = -128;integer Integer_b_one =-128; System.out.println (Integer_a_one = = Integer_b_one);//Trueinteger Integer_a_two = 127;integer Integer_b_two = 127; System.out.println (Integer_a_two = = integer_b_two);//True        integer Integer_c_one = -129;integer Integer_d_one =- 129; System.out.println (Integer_c_one = = Integer_d_one);//Falseinteger Integer_c_two = 128;integer integer_d_two = 128; System.out.println (Integer_c_two = = integer_d_two);//False</span>


Summary: The basic data type corresponding packaging type automatic packing pool is a certain size, see the source code can understand the next

Int,byte,short,long corresponds to -128~127.

Character corresponds to 0~127 .

float and double do not have an auto-boxing pool (see code)

<span style= "Font-family:simsun;font-size:18px;color: #000000;" >              Float fa=3f; Float fb=3f; System.out.println (FA==FB);//falsedouble da=3d;double db=3d; System.out.println (DA==DB);//false</span>




Automated unpacking and boxing in Java

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.