Java basic data type packaging class, java data type packaging
Basic data type packaging
When a program requires an int age to be input interactively, the input result from the text box must be of the String type. To perform related operations in the program, it must first be converted to the int type. Therefore, you can use data type conversion or forced conversion. However, this conversion method is too simple and has few functions. Therefore, java provides the concept of data type packaging class.
Data Type packaging class refers to the ability to convert data into objects (packaging), and to obtain data (unpackaging) from data objects ). It is equivalent to a layer of data packets with unchanged content, but since it is an object, there are a lot of data operation methods. The packaging process is to copy the data in the stack space to the object in the heap space. The content of this object is the required data.
The packaging class corresponding to byte is Byte;
Short --> Short;
Int --> Integer;
Long --> Long;
Float --> Float;
Double --> Double;
Boolean --> Boolean;
Char --> Character;
Most of these data type packaging functions are similar, and most of the methods provided are similar. One of the most important functions is:Conversion between data types and String types .★★★★★
Taking Integer packaging as an example:
Basic functions: automatic packing and automatic unpacking features:
To operate on a value, you need to define a variable of the int type:int i = 4;To use some general methods of the Integer class, you need to define an Integer class object.Integer i = new Integer(4);. To execute a method on an int variable, encapsulate it as an Integer class object, or, in turn, convert it to an int data type to subtract, multiply, and divide the Integer object, it is extremely troublesome to switch back and forth between them.
Jdk 1.5 and later versions Support automatic packing and unpacking.
Integer i = 4;
Define a reference type variable I, the reference type can only point to the object, SO 4 is automatically converted to the object, so the right side of the equal sign is equivalent to using Integer. valueOf (4 ). The new Integer () is actually called in the Integer. valueOf () override method (). Therefore, the following statements are equivalent:
Integer i = 4;Integer i = new Integer(4);Integer i = Integer.valueOf(4);
On the contrary, automatic unpacking means automatically extracting values from objects.
Integer i = 4;i = i + 5;
Here, "I + 5" is a numerical operation. I first converts the object to a numerical value and obtains 9. The I on the left of the equal sign is an object reference, so we need to automatically pack 9 as an object. So it is equivalent to the following code:
i = Integer.valueOf(i.intValue()+5);
It can be seen that the feature of automatic packing and unpacking greatly enhances readability.
But the automatic packing process has a little detail: When the value is within the byte range (-128-127), no new objects will be created during automatic packing. It means that when the value of an Integer x object is 3, it is smaller than 127. If there is an Integer y, and its value is also 3, then x and y point to the same object.
Integer x = new Integer (3); Integer y = new Integer (3); System. out. println (x = y); // falseSystem. out. println (x. equals (y); // trueInteger a = 3; Integer B = 3; System. out. println (a = B); // trueSystem. out. println (. equals (B); // trueInteger c = 128; Integer d = 128; System. out. println (c = d); // falseSystem. out. println (c. equals (d); // true
Java Communication Group 669823128