Automatic packing and unpacking of Java

Source: Internet
Author: User

Click here to get the original English document Link

Today, when I looked at generics, I learned about the new features of JDK, including automatic packing and unboxing. below is my understanding of the original English document.

Any JavaProgramMembers know that you cannot put an int type (or other basic types) into a collection, and only object references can be loaded in the collection, so we need to wrap the basic type int (the int packaging class is integer ). When you extract an integer object from a set, you must call the intvalue () method to unpackage the integer object. Frequent unpacking and unpacking make it very painful.CodeIt looks messy. The automatic packing and unpacking process is automated, minimizing the above troubles.

 
Import Java. util. *; // prints a frequency table of the words on the command linepublic class frequency {public static void main (string [] ARGs) {Map <string, integer> M = new treemap <string, integer> (); For (string word: ARGs) {INTEGER freq = m. get (Word); M. put (word, (freq = NULL? 1: freq + 1);} system. Out. println (m );}}

the explanation of this Code is written according to your own understanding. There is no translation. In treemap, the object type is stored, now, the int type is automatically converted into an integer and saved to the map. when the data is accumulated, the integer type is converted into an int type for unboxing. Without this step, we need to tedious write as integer
I = new INTEGER (freq) for Integer Conversion and remove freq when adding. intvalue.

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.