[J2SE 5.0 topic] automatic packing and automatic unpacking

xiaoxiao2021-03-06  19

I have seen the .NET foundation may not be unfamiliar with this packing and unpacking concept. First, we must clarify that in Java, there are two distinct data types: value types and reference types. The data of the value type is not an object, so the memory and resources occupied are relatively small, but it cannot be called to call it to Tostring (), havehcode (), getclass (), equals (), etc., can not be added directly In the collection; the data of the reference type is an object, which takes up more memory and resources, but provides a rich access method, as a real object, can be placed directly into the collection. The so-called packing is to package the value type with the reference type of the reference type, so that they can have the identity of the object. If we can pack the INT type into an object of the Integer class, or package the double to double, and so on. The so-called unpacking is in contrast to the direction of the box, re-simplifies the object of the reference type of Integer and Double to the value type data. Before J2SE 5.0 is released, we can only handle the boxes and unpacks, and now, the compiler can help us complete these necessary steps. The following code I offer two versions of the box and unboxes, one version uses manual way, another version handed these obvious code to the compiler to complete: public static void manboxingunboxing (int i) {arraylist alist = new arraylist (); alist.add (0, new integer (i)); int a = alist.get (0) .intValue (); System.out.Println ("The value of i is " a);

Public static void autoboxingunboxing (INT i) {arraylist alist = new arraylist (); alist.add (0, i); int a = alist.get (0); system.out.println ("THE Value of i is " a);} See it, in J2SE 5.0, we no longer need to explicitly turn a value type of data into the corresponding object, thereby transmitting it as an object to other methods, too It is not necessary to manually remove the object that represents a value to the corresponding value type data. Of course, there is a problem with a problem. You must do some instructions: For value types and reference types, there is still a significant difference in resource, while using this convenient feature, don't make simple forgetting them in nature. For J2SE 5.0 automatic packing and unpacking details, refer to here.

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

New Post(0)