This is the 26th day of my participation in the August Text Challenge.More challenges in August
preface
- 8.26
- Programming learning doesn’t stop
- To become a senior talent, programming and operation which can not be less
- All give me learn!!
- In the to
The target
What will we learn about packaging classes
Why wrapper classes
What wrapper classes are provided by Java
An introduction to common methods and constants for various wrapper classes
What is packing operation and what is unpacking operation and so on
1. What is packaging
Java has eight basic data types, and each of these basic types in Java is wrapped into a class, called a wrapper class.
The wrapper class can be divided into three types: Number, Character and Boolean. The architecture diagram of the wrapper class is shown as follows:
2. Why is the wrapper class needed
We know that Java is an object-oriented programming language, but for developers to get started, Java uses the basic data types of C language, so Java data types are divided into basic data types and reference data types.
For simple operations, developers can use primitive data types directly. But for scenarios that require objectified interactions (such as storing primitive data types into collections), you need to encapsulate the primitive data types as Java objects because they do not have some of the characteristics of objects, do not have object properties and methods, and cannot use object-oriented programming ideas to organize code. For this reason, wrapper classes are created.
A wrapper class is just a class, so it has properties, methods, and object interactions.
3. Basic data types and wrapper classes
The following table lists the wrapper classes for the basic data types. These wrapper classes are all in the java.lang package, so we don’t need to import them manually when using wrapper classes.
Basic data types | Corresponding wrapper class |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Except for Integer for int and Character for char, the other six data types are named with the uppercase first letter of their base data types.
4. Common methods for wrapping classes
4.1 Number class
The Number class is the parent of all numeric wrapper classes. Here, we take one of the subclasses Integer as an example to introduce its constructor, common methods, and constants.
4.1.1 Construction method
The Integer class provides two constructors:
Integer(int value)
: creates an Integer object using a variable of type int as a parameter;Integer(String s)
: Creates an Integer object that takes a String variable as an argument.
Examples are as follows:
package com.caq.exception;
public class Test2 {
public static void main(String[] args) {
// Create an object with an int variable as an argument
Integer integer = new Integer(3);
// Create an object with a string type as an argument
Integer integer1 = new Integer("5"); }}Copy the code
4.1.2 Common methods
byte byteValue()
: Returns the Integer value as byte;int compareTo(Integer1 Integer2)
: Compares two Integer objects numerically. If these two values are equal, 0 is returned; If the value of the calling object is less than the value of Integer2, a negative value is returned; If the value of the calling object is greater than the value of Integer2, a positive value is returned;boolean equals(Object obj)
: compares whether this object is equal to the specified object.int intValue()
: Returns this Integer object as an int;int shortValue()
: Returns this Integer object as short;toString()
: returns a String representing the Integer value;static Integer valueOf(String str)
: returns an Integer image that holds the specified String value.int parseInt(String str)
: Returns the integer equivalent of the number contained in the string specified by STR.
4.1.3 Common Constants
MAX_VALUE
: represents the maximum value that int can take;MIN_VALUE
: represents the minimum value that int can take;SIZE
: represents the number of bits representing an int value in the form of binary complement;TYPE
: indicates the basic Class instance.
package com.caq.exception;
public class Test2 {
public static void main(String[] args) {
int a = Integer.MAX_VALUE;
int b = Integer.MIN_VALUE;
int c = Integer.SIZE;
System.out.println("The MAX_VALUE of int is desirable"+a);
System.out.println("The desirable MIN_VALUE of int is"+b);
System.out.println("What are the binary digits that are acceptable for int?"+c); }}Copy the code
Running results:
Int MAX_VALUE is 2147483647. Int MIN_VALUE is -2147483648. Int 32Copy the code
4.2 Character class
The Character class wraps a value in an object of the basic type char. A Character object contains a single field of type CHAR.
4.2.1 Construction method
The Character class provides a constructor:
Character(char value) : Rarely used.
4.2.2 Common methods
char charValue()
: returns the value of this Character object;int compareTo(Character anotherCharacter)
: Returns the value of this Character object, comparing two Character objects numerically, or 0 if they are equal;boolean equals(Object obj)
: compares the object on which this method is called with the specified object;char toUpperCase(char ch)
: Converts character arguments to uppercase;char toLowerCase(char ch)
: Converts character arguments to lowercase;String toString()
: returns a String representing the specified char value;char charValue()
: returns the value of this Character object;boolean isUpperCase(char ch)
: Checks whether the specified character is uppercase.boolean isLowerCase(char ch)
: Checks whether the specified character is lowercase.
4.3 Boolean class
Boolean classes wrap values of basic type Boolean in an object. An object of type Boolean contains only one field of type Boolean. In addition, this class provides many methods for converting Boolean and String to and from each other, as well as other constants and methods that are useful when working with Boolean.
4.3.1 Construction method
The Boolean class provides the following two constructors:
Boolean(boolean value)
: Creates a Boolean object representing the value argument (rarely used);Boolean(String s)
: Creates a Boolean object that takes the String variable as an argument. In this case, if the string passed is not null and the case insensitive content is equal to “true”, the generated Boolean object value is true, otherwise false. (Rarely used).
4.3.2 Common methods
boolean booleanValue()
: Returns the value of a Boolean object as the corresponding Boolean value;boolean equals(Object obj)
: Checks whether the object calling this method is equal to obj, and returns true if and only if the argument is not null and both Boolean objects represent the same Boolean value as the object calling this method;boolean parseBoolean(Sting)
: parses string arguments to Boolean values;String toString()
: returns a String representing the Boolean value;boolean valueOf(String s)
: returns a Boolean value representing the value with the specified string.
4.3.3 Common Constants
TRUE
: Boolean object corresponding to the base value true;FALSR
: Boolean object corresponding to base value false;TYPE
: indicates the basic Class instance.
5. Packing and unpacking
- Boxing is the conversion of basic data types to packaging classes;
- Unpacking is a wrapper class converted to a primitive data type.
- Packing and unpacking are divided into automatic and manual.
package com.caq.exception;
The java.lang package is the core of the Java language and provides the base classes in Java.
// Include basic Object Class, Class Class, String Class, basic type wrapper Class, basic math Class and so on the most basic classes.
// The application of classes in the lang package does not require manual import.
public class Test2 {
public static void main(String[] args) {
// Automatic boxing
int num1 = 10;
Integer num2 = num1;
System.out.println(num2);
// Manually encase
Integer num3 = new Integer(10); System.out.println(num3); }}Copy the code
10
10
Copy the code
- Automatic boxing is to directly assign a variable of a basic data type to a variable of the corresponding packaging type.
- Manual boxing simply calls the constructor of the wrapped class (not recommended)
Split open a case
package com.caq.exception;
The java.lang package is the core of the Java language and provides the base classes in Java.
// Include basic Object Class, Class Class, String Class, basic type wrapper Class, basic math Class and so on the most basic classes.
// The application of classes in the lang package does not require manual import.
public class Test2 {
public static void main(String[] args) {
// Auto-plug
Integer num1 = 20;
int num2 = num1;
System.out.println(num2);
// Manually unpack
intnum3 = num1.intValue(); System.out.println(num3); }}Copy the code
20
20
Copy the code
Automatic unpacking is to directly assign a variable of packaging type to the corresponding basic data type variable;
Manual unpacking is done by calling the xxxValue() method under the corresponding wrapper class.
6. Summary
- A wrapper class is a class that wraps basic data types with properties, methods, and object interactions.
- In addition to
int
The corresponding wrapper class name isInteger
As well aschar
The corresponding wrapper class nameCharacter
, other6
The wrapper classes corresponding to each data type are named with the uppercase first letter of their base data type. - Packing is the basic data type to the packaging class conversion, unpacking is the packaging class to the basic data type conversion.