Chapter 2 Packaging
We know that Java is an object-oriented language, and one of its essences is that polymorphism can be used to improve the flexibility of the program.
But there are 8 basic types in java: byte, short, int, long, float, double, char, boolean. They exist in memory as values, not objects.
They are not subclasses of Object and cannot participate in object-oriented development. Before the Java 1.5 version, the following code could not pass.
package com.souvc.api;public class Test { public static void main(String[] args) {
String str = "hello";
doSome(str);// 可以,因为String是Object的子类
int i = 1; // 程序编译不通过,原因在于int不是Object的子类,不能使用多态性。 doSome(i);
} public static void doSome(Object o) { // .... }
}The reason for the problem is that the basic type int has no inheritance relationship and is not a subclass of Object. Therefore, if we want the basic type to exist in the form of an object, we need to define a class Integer, and then use its instance to describe a basic type int.
The advantage of this is that we use objects to describe basic type data, and classes inherit from Object. This allows basic types to participate in object-oriented development. Fortunately, classes like Integer do not need to be defined by us, because Java already provides wrapper classes corresponding to 8 basic types.
Note: The automatic unboxing feature appeared after version 1.5 of Java, and the above code can be compiled normally. We will describe the automatic unpacking and packing of boxes in detail later.
For 8 basic types, java provides their corresponding packaging classes:
Basic type packaging class
byte java.lang.Byte
short java.lang.Short
int java.lang. Integer
long java.lang.Long
float java.lang.Float
double java.lang.Double
char java.lang.Character
boolean java.lang.Boolean
Except Character and Boolean Except for the parent class Object, the rest are inherited from: java.lang.Number
Number is an abstract class. It cannot be instantiated itself. Subclasses of Number must provide methods to convert the represented value to byte, double, float, int, long and short
For example:
abstract double doubleValue() returns the specified value in double form
abstract int intValue() Returns the specified numerical value in the form of int
abstract float floatValue() Returns the specified numerical value in the form of float
For the remaining abstract methods, please refer to the API documentation: java.lang.Number.
Now let’s learn how to convert between basic types and wrapper classes.
When we need to convert the basic type to a wrapper class, we can call a static method valueOf() of the wrapper class:
Integer i = Integer.valueOf(1);
Double d = Double.valueOf(1.1 :
Double d = new Double(1.1);double dn = d.doubleValue();
Although we can pass the above Methods convert between basic types and wrapper classes. But it is relatively troublesome when actually writing code. Java launched a new feature after version 1.5: automatic unboxing.
Integer in = 1;//You can automatically convert basic types into packaging classes. Automatic boxing
So how does Java implement automatic unboxing?
2.1.1 Constructor method - constructor method of Integer class 34
2.1.2 Constant - constant of Integer class 34
2.1.3 bitCount method - get the number of 1 in two's complement 34
2.1.4 byteValue method ——Get the value of byte type 35
2.1.5 CompareTo method——Compare integer 35
2.1.6 Decode method——Decode the string into int type 35
2.1.7 DoubleValue method——Return the double value 36
2.1.8 equals method - determine the equality of integer objects 36
2.1.9 FloatValue method - get the float value 37
2.1.10 GetInteger method - get the system attribute value of the integer 37
2.1.11 hashCode method - generate the hash code of the integer 39
2.1.12 highestOneBit method - Get the index of the highest bit 1 of the integer binary 39
2.1.13 intValue() method - get the int value 40
2.1.14 longValue method - get the long value 40
2.1.15 lowestOneBit method ——Get the index of the lowest binary bit 1 of the integer 41
2.1.16 parseInt method——Parse the string into an int value 41
2.1.17 reverse method——Reverse the bit order of the two’s complement of the integer 43
2.1.18 reverseBytes Method - Reverse the order of integer bytes 44
2.1.19 | shortValue method - get the short value 44
2.1.20 | signum method - get the integer sign 44
2.1.21 | toBinaryString method - generate a binary string of integers 45
2.1.22 toHexString method - generates a hexadecimal string of integers 45
2.1.23 toOctalString method - generates an octal string of integers46
2.1.24 toString method - generates a decimal string of integers47
2.1 .25 ValueOf method - Create Integer object 49
2.2 Long class - Long integer class 50
2.2.1 Constructor method - Constructor method of Long class 51
2.2.2 Constant - Constant of Long class 51
2.2.3 bitCount method - get the number of 1's in two's complement 51
2.2.4 byteValue method - get the byte value 51
2.2.5 compareTo method - compare long integers 52
2.2.6 decode method—— The string is decoded into long type 52
2.2.7 | doubleValue method - returns the double value 53
2.2.8 | equals method - determines the equality of long integer objects 53
2.2.9 | floatValue method - gets the float value 54
2.2.10 | getLong Method - Get the system attribute value of the long integer 54
2.2.11 | hashCode method - Generate the hash code of the long integer 56
2.2.12 | highestOneBit method - Get the index of the highest binary bit 1 of the long integer 56
2.2.13 | intValue () method - get the int value 57
2.2.14 longValue method - get the long value 57
2.2.15 lowestOneBit method - get the index of the lowest bit 1 of the long integer binary 57
2.2.16 parseLong method - convert the string Parsed as a long value 58
2.2.17 Reverse method - Reverse the bit order of the two's complement of a long integer 60
2.2.18 ReverseBytes method - Reverse the order of the long integer bytes 60
2.2.19 ShortValue method - Get short value 61
2.2.20 signum method - Get the long integer sign 61
2.2.21 |toBinaryString method - generate a long integer binary string 61
2.2.22 toHexString method - generate a long integer hexadecimal string 62
2.2.23 toOctalString method - generates an octal string of long integer 62
2.2.24 toString method - generates a decimal string of long integer 63
2.2.25 valueOf method - creates a Long object 65
2.3 Short Class - short integer class 67
2.3.1 Constructor - constructor method of Short class 67
2.3.2 Constant - constant of Short class 67
2.3.3 compareTo method - compare short integers 67
2.3 .4 decode method - decode the string into short type 68
2.3.5 |doubleValue method - return the double value 68
2.3.6 |equals method - determine whether short integer objects are equal 68
2.3.7 | floatValue method - get the float value 69
2.3.8 hashCode method - generates a hash code of a short integer 69
2.3.9 intValue() method - obtains an int value 70
2.3.10 longValue method - obtains a long value 70
2.3.11 parseShort method— —Parse the string into a short value 70
2.3.12 reverseBytes method—reverse the order of short integer bytes72
2.3.13 shortValue method—get the short value 72
2.3.14 toString method—generate a short integer Decimal string 73
2.3.15 ValueOf method - Create Short object 73
2.4 Boolean class - Boolean class 75
2.4.1 Constructor - Constructor method of Boolean class 75
2.4.2 Constant - Constants of the Boolean class 75
2.4.3 | booleanValue method - Get boolean value 76
2.4.4 | compareTo method - compare Boolean values 76
2.4.5 | equals method - determine equality 77
2.4.6 | getBoolean method - get Boolean Type system attribute value 77
2.4.7 hashCode method - generates a hash code of a Boolean object78
2.4.8 parseBoolean method - parses a string into a boolean value78
2.4.9 toString method - generates a Boolean value String 78
2.4.10 valueOf method - Create Boolean object 79
2.5 Byte class - byte object 80
2.5.1 Constructor - constructor of Byte class 80
2.5.2 Constant - constant of Byte class 80
2.5.3 compareTo method - compare bytes Object 80
2.5.4 decode method - decode the string into Byte value 81
2.5.5 |doubleValue method - get the double value 82
2.5.6 equals method - determine byte equality 82
2.5.7 floatValue method - — Get the float value 83
2.5.8 | hashCode method - generate the hash code of the byte object 83
2.5.9 | intValue method - get the int value 83
2.5.10 | longValue method - get the long value 83
2.5.11 parseByte method - parse the string into a byte value 84
2.5.12 | shortValue method - get the short value 85
2.5.13 | toString method - generate a decimal string of byte values 85
2.5.14 | valueOf method - create Byte object 86
2.6 Character class - character class 88
2.6.1 Constructor method - constructor method of Character class 88
2.6.2 Constant - constant of Character class 88
2.6.3 charCount method - Calculate the number of code points of the specified character 89
2.6.4 | charValue method - get the char value 89
2.6.5 | codePointAt method - get the code point of the character array element 90
2.6.6 | codePointBefore method - get the previous character array index The code point of the element 91
2.6.7 The codePointCount method - returns the number of code points in the subarray of the character array 93
2.6.8 The compareTo method - compares character objects 94
2.6.9 The equals method - determines whether the character objects are equal 95
2.6.10 getNumericValue method - returns the int value represented by the character95
2.6.11 getType method - returns a value indicating the general category of the character97
2.6.12 hashCode method - generates the hash code of the character object97
2.6.13 isDefined method - determine whether it is a Unicode character 98
2.6.14 isDigit method - determine whether it is a numeric character 98
2.6.15 isLetter method - determine whether it is an alphabetic character 99
2.6.16 isLowerCase method - determine Whether it is a lowercase character 100
2.6.17 isUpperCase method - determine whether it is an uppercase character 100
2.6.18 toLowerCase method - convert to a lowercase character 101
2.6.19 |toUpperCase method - convert to an uppercase character 101
2.7 Double ——Double precision number class 102
2.7.1 Constructor method——Constructor method of Double class 102
2.7.2 Constant——Constant of Double class 102
2.7.3 byteValue method——Get byte value 102
2.7. 4. compare method - compare double precision digital objects 103
2.7.5 compareTo method - compare two Double objects 103
2.7.6 intValue method - return this Double value in int form 104
2.7.7 doubleToLongBits method - Return the representation of the specified floating point value 104
2.7.8 | doubleToRawLongBits method - retain NaN values and return the representation of the specified floating point value 105
2.7.9 | doubleValue method - get the double value 105
2.7.10 | equals method - judge Whether Double objects are equal106
2.7.11 floatValue method--obtain float value 107
2.7.12|hashCode method--generate the hash code of Double object107
2.7.13|isInfinite method--determine whether the size of Double value is infinite107
2.7.14 isNaN method - determine whether the Double value is a non-numeric value 108
2.7.15 longBitsToDouble method - return the double value of the given bit representation 109
2.7.16 longValue method - get the long value 110
2.7. 17. The parseDouble method - parses the string into a double value 110
2.7.18 The shortValue method - gets the short value 110
2.7.19 The toHexString method - generates a hexadecimal string of double precision numbers 111
2.7.20 toString Method - Generate a decimal string of double precision numbers 112
2.7.21 ValueOf method - Create a Double object 112
2.8 Float - Floating point class 113
2.8.1 Constructor method - Constructor method of Float class 113
2.8.2 Constants - Constants of Float class 114
2.8.3 byteValue method - Get byte value 114
2.8.4 Compare method - Compare Float objects 114
2.8.5 compareTo method - compares the values represented by two Float objects 115
2.8.6 doubleValue method - obtains the double value 115
2.8.7 equals method - determines whether Double objects are equal 115
2.8.8 floatToIntBits method ——Return the representation of the floating point value 116
2.8.9 floatToRawIntBits method——Keep the non-numeric value and return the representation of the specified floating point value 117
2.8.10 floatValue method——Get the float value 118
2.8.11 hashCode method— — Returns the hash code of the Float object 118
2.8.12 intBitsToFloat method — returns the float value in the specified bit representation 118
2.8.13 intValue method — gets the int value 119
2.8.14 isInfinite method — determines the float value Whether the size is infinite 120
2.8.15 | isNaN method - determine whether the Float value is a non-numeric value 120
2.8.16 | longValue method - get the long value 121
2.8.17 | parseFloat method - parse the string into a float value 121
2.8.18 shortValue method--get the short value 122
2.8.19 toHexString method--generate a hexadecimal string of floating-point numbers 122
2.8.20 toString method--generate a decimal string of floating-point numbers 123
2.8 .21 ValueOf method - Create floating point object 124
The above is the content of the packaging class. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20448
7
13592
4
How to parse XML in Ruby on Rails? (Web Frameworks)
Jan 05, 2026 am 12:53 AM
UseNokogiriforfast,robustXMLparsinginRails:installviagem'nokogiri',parsewithNokogiri::XML,handleencoding/namespacesexplicitly,andextractdatasafelyusingcss()/xpath()whilecheckingdoc.errorsand.textguards.
How to compare two XML files and find differences
Jan 05, 2026 am 01:05 AM
Use tools or programming methods to compare XML file differences: first use online tools (such as xmlcompare.org) or IDE plug-ins for visual comparison, and then implement automated analysis through the command line (such as xmllint diff) or Python scripts (using the lxml library to parse and standardize XML). Pay attention to the processing of whitespace characters, attribute order, namespace prefixes and other influencing factors. It is recommended to use CanonicalXML to eliminate format differences.
How to fix XML encoding issues (UTF-8)? (Character Sets)
Jan 03, 2026 am 02:48 AM
XMLshowsgarbledtextwhendeclaredUTF-8encodingmismatchesactualbyteencoding;verifywithfile-iorxxd,fixbyresavingcorrectly,andparseinbinarymodetohonortheXMLdeclaration.
The Role of XML in modern configuration files (e.g., pom.xml, web.config)
Jan 02, 2026 am 12:59 AM
XMLremainswidelyusedinmodernconfigurationfilesduetoitsstructured,hierarchicaldatarepresentation,enablingclearparent-childrelationshipsandmetadatastorageviatagsandattributes,asseeninpom.xmlandweb.config.2.ItsupportsformalvalidationthroughXSDandDTDsche
How to serialize C# objects to XML? (Data Persistence)
Jan 04, 2026 am 01:41 AM
XmlSerializerisidealforsimple,attribute-drivenXMLserializationinC#,requiringpublicproperties,parameterlessconstructors,andconcretetypes;itignores[Serializable],supportsListbutnotDictionary,lackscircularreferencehandling,andneedsexplicitnull/namespace
How to manage dependencies with a pom.xml in Maven
Jan 03, 2026 am 01:42 AM
The pom.xml file declares dependencies through groupId, artifactId and version, and can use scope to specify the scope; 2. Multi-module projects use dependencyManagement to unify versions; 3. Maven automatically handles transitive dependencies, and exclusions can be used to exclude conflicting libraries; 4. Use MavenVersionsPlugin to check dependency updates to keep projects safe and consistent.
How to delete specific nodes from XML in Java? (DOM Manipulation)
Jan 07, 2026 am 12:14 AM
To delete a specific node, you need to operate based on the tag name and conditions (such as attribute values or text content): the liveNodeList must be traversed in reverse order and removeChild() is called; XPath is recommended for complex conditions; be sure to check that the parent node is not empty before deleting to avoid NPE; performance and memory limitations should be considered for large-scale deletions.
How to deserialize XML into a C# object
Jan 02, 2026 am 12:45 AM
Use the XmlSerializer class to deserialize XML into a C# object. You need to define a class that matches the XML structure and annotate it with attributes such as XmlElement and XmlAttribute. Then read the XML stream and convert it into an object instance through the Deserialize method.




