J2SE 5.0 instance --- Autoboxing and Auto-unboxing of native types

xiaoxiao2021-03-06  15

The native type of autoboxing and auto-unbo we know that in Java, int, long and other native types are not a class inherited from Object, so there are many operations we can't use native types, such as wanting an integer. In a collection, we must first create an Integer object, then put this object into the collection. When we take a number from the collection, we take an Integer object, so it cannot be directly used to use an additional and subtraction and division and other operators, but it must be taken with Integer.intValue () to take the corresponding value, this The process is called Boxing and Unboxing.

J2se5.0 supports autoboxing and auto-unboxing, that is, we don't need to make these boxing and unboxing operations in the future, Java language will be done for us. Specifically, you can refer to the example below:

List intlist = new arraylist (); intlist.add (2); intlist.add (new integer (5)); int i = 3 IntList.get (0); // i = 5int J = 3 Intlist.get (1); // j = 8

From this program we can see that autoboxing and auto-unboxing saves us a lot of unnecessary work.

转载请注明原文地址:https://www.9cbs.com/read-45834.html

New Post(0)