J2SE 5.0 Examples---native types of autoboxing and auto-unboxing

Source: Internet
Author: User
Tags integer
Autoboxing and auto-unboxing of J2SE primitive types
We know that in Java, the native type such as Int,long is not a class that inherits from object, so there are many operations that we cannot use primitive type operations, such as trying to put an integer into a set, we must first create an integer object, The object is then put into the collection. When we take the number from the collection, we take out an integer object, so we can't use subtraction operator directly, but we must use Integer.intvalue () to get the corresponding value, such process called boxing and unboxing.

j2se5.0 supports autoboxing and auto-unboxing, which means that we don't have to do these boxing and unboxing operations manually anymore, and the Java language will do it for us. You can refer to the following example:

List<integer> intlist=new arraylist<integer> ();



Intlist.add (2);



Intlist.add (New Integer (5));



int I=3+intlist.get (0);//i=5



int J=3+intlist.get (1); J=8

From this procedure we can see that autoboxing and auto-unboxing have spared us a lot of unnecessary work.


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.