Java design promotes the idea that everything is an object. But from the division of data types, we know that Java data types are divided into basic data types and reference data types, but how can basic data types be called objects? So Java designs separate Classes for each basic data type, called Wrapper Classes, or in some places, Wrapper Classes or data type Classes. The relationship between wrapper classes and base data types is shown in the following table.

The serial number Basic data types A wrapper class
1 byte Byte
2 short Short
3 int Integer
4 long Long
5 char Character
6 float Float
7 double Double
8 boolean Boolean

As you can see from the table above, the names of the other six types are well understood, except for the names defined by Integer and Character, which differ significantly from those defined by the base data types.

Packing and unpacking

After understanding the packaging class, the following introduces the concept of packing and unpacking the packaging class. The process of converting a basic data type into a wrapper class is called boxing, for example, wrapping an int into an object of the Integer class. The process of wrapping a class into a primitive data type is called unboxing, such as resimplifying an object of the Integer class to an int.

Manually instantiating a packaging class is called manual unboxing boxing. Before Java version 1.5, manual unpacking was required. After Java version 1.5, automatic unpacking can be implemented. That is, when converting basic data types and corresponding packaging classes, the system will automatically unpack and unpack the data, which provides more convenience for developers. Such as:

public class Demo { public static void main(String[] args) { int m = 500; Integer obj = m; Int n = obj; Println ("n = "+ n); // System.out.println("n =" + n); Integer obj1 = 500; System.out.println("obj equals obj1 returns "+ obj. Equals (obj1)); }}Copy the code

Running results:

N = 500 obj is equivalent to obj1 returning trueCopy the code

Automatic unpacking and packing is a common function, which needs to be mastered.

Application of the wrapper class

The following eight wrapper classes are used, and the following are common application scenarios.

1) Realize the conversion between int and Integer

Int can be boxed through the constructor of the Integer class and unboxed through the intValue method of the Integer class. Such as:

public class Demo { public static void main(String[] args) { int m = 500; Integer obj = new Integer(m); Int n = obj.intValue(); System.out.println("n = "+ n); Integer obj1 = new Integer(500); System.out.println("obj equals obj1 + obj.equals(obj1)); }}Copy the code

Running results:

N = 500 obj is equivalent to obj1 returning trueCopy the code

2) Convert a string to a numeric type

The Integer and Float classes provide the following two methods:

int parseInt(String s);
Copy the code

S is the string to be converted. Float class (String to Float)

float parseFloat(String s)
Copy the code

Note: When using the above two methods, the data in the string must consist of numbers, otherwise the conversion will result in a program error. The following is an example of converting a string to a numeric type:

public class Demo { public static void main(String[] args) { String str1 = "30"; String str2 = "30.3"; Int x = integer.parseint (str1); // Change string to float float f = float. ParseFloat (str2); System.out.println("x = "+ x +"; f = " + f); }}Copy the code

Running results:

X = 30; F = 30.3,Copy the code

3) Convert integers to strings

The Integer class has a static toString() method that converts integers to strings. Such as:

public class Demo { public static void main(String[] args) { int m = 500; String s = Integer.toString(m); System.out.println("s = " + s); }}Copy the code

Running results:

s = 500
Copy the code

Java Learning Video

Java basis:

Learn Java, make learning a pleasure

Java project:

【Java Games Project 】1 hour to teach you how to make classic Minesweeper games in Java

【Java graduation design 】OA office system project combat _OA staff management system project _Java development