The 8 basic data types in Java are described earlier, including Boolean, Byte, char, short, int, long, float, double. Java also provides these types of encapsulation classes, Boolean, Byte, Character, short, Integer, Long, Float, Double, respectively. These encapsulation classes are placed under the Java.lang package.
1. Why do I need a basic type of encapsulation class?
By viewing the data and personal analysis, the difference between the package class and the basic type can be attributed to the following 4 points:
1) int is a numeric type that is oriented to the bottom of a machine, is a data type of type primitive, is generally used only in numerical calculations, and integer is the Warpper class of int, which is the object type of OOP, which is used in other places of Java to use objects. For example, the map key and the element value,list and set are used to wrap int as an integer object to hold the numeric information. int generally as a numeric parameter is enough, integer generally do type conversion when used more.
2) basic data types have default values and cannot express empty conditions. An integer can distinguish between unassigned and a value of 0, and int cannot express an unassigned condition. For example, if you want to express the difference between not taking an exam and 0 of your exam results, you can use only integer. In JSP development, the default for integer is null, so when displayed in a text box with an El expression, the value is a blank string, and the default value of int defaults to 0, so when the El expression is displayed in the text box, the result is 0, so that int does not work as a type of form data for the Web tier.
3) int is the base type, and integer is the reference type. Encapsulation classes need to involve the instantiation and recycling of objects, which is less efficient than the basic data types. The encapsulation class implements many tool methods for manipulating basic data types and the maximum minimum value constants for integers, which are better than the basic data types.
4) The storage location is different, the reference of the object enclosing the class is placed inside the stack, and the instance of the object is placed inside the heap. The basic data type is a member variable when placed in the heap, is a local variable when placed inside the stack (method stack).
2, the Java automatic packing, unpacking
Starting with Java 5, the automatic boxing/unpacking mechanism has been introduced to enable quick and easy conversion between the basic data types and their encapsulation classes
Boxing : The basic types are packaged with their corresponding reference types, making them the object's nature.
such as: Integer a = 100; This is auto-boxing (the compiler calls the valueof (int i) method of the static integer)
unpacking : In contrast to boxing, simplifying objects of reference types to data of value types
such as: int b = new Integer (100); This is the automatic unpacking (the compiler calls the integer Intvalue () method)
Boxing mechanism There is a small trap to note
Public Static void Main (String arg[]) { = 123; = 123; System.out.println ("I1 equal i2:" + (i1.equals (I2))); System.out.println ("i1 = = i2:" + (I1 = = i2)); }
After executing the above code, we will find the following magical results:
Truetrue
Check the source code quickly found the problem, the source code is as follows (of which low=-128,high=127)
Public Static Integer valueOf (int i) { if (i >= integercache.low && i <= integercache . High) return integercache.cache[i + (-integercache.low)]; return New Integer (i); }
It turns out that Java automatically boxed the value between -128~127 in the cache, by automatically boxing the reference to the fixed cache address, instead of allocating new storage space (this phenomenon in Byte, short, Integer, Long, Character these packaging classes are like this, interested to see the source code can be known).
Believe that the small partners after reading the above code all began to doubt life, feel that their previous "= =" and "equal" understanding is not wrong, and then give the normal logical results of the code
Public Static void Main (String arg[]) { =-n ; =; System.out.println ("I1==I2:" + (I1 = = i2)); System.out.println ("I1 equal i2:" + i1.equals (i2)); New Integer (1); New Integer (1); System.out.println ("I3==I4:" + (i3 = = i4)); System.out.println ("i3 equal I4:" + I3.equals (i4)); }
Run results
Falsetruei3falsetrue
Haha, the world is still good, I believe that the mood is much more beautiful now.
As can be seen from the above code and running results, when the auto-boxed value is not between -128~127, the Java virtual Opportunity allocates a new storage space for the object, that is, the new object.
3. Method of Encapsulation class
1) Construction Method: Each package class will provide a constructor with basic data type
such as Boolean (Boolean value), Byte (byte value), Character (char value), and so on, are used to manually sub-load the corresponding base data type.
2) Disassembly method
such as Booleanvalue (), Bytevalue (), Charvalue, and so on, convert the reference type to the corresponding base data type.
such as parsefloat (string s), Parsebyte (string s), and so on, convert the String type to the corresponding base data type, parsefloat ("3.14") into the float type.
3) Encapsulation method (divided into two categories)
such as ValueOf (boolean value), ValueOf (byte value), ValueOf (char value), and so on, the base data type is converted to the corresponding wrapper class.
If ValueOf (string s) converts the string type to the corresponding package type, float.valueof ("3.14") converts the string "3.14" to the Float type.
4) Method of string
such as ToString (), ToString (Float value), and so on, the package type is converted to the STIRNG type.
5) Comparison method
If CompareTo () is used to compare two value sizes, equals () compares two values for equality.
The above are some common methods of encapsulating classes for basic data types, and are often used in daily work. Each package class has its own unique approach, with time to study it on its own.
Java from Basics (a) data type (bottom)