Second, Java object-oriented (13) _ basic type wrapper class

2018-05-03

 

Basic type wrapper class

 

1. Packing and unpacking

Although the Java language is a typical object-oriented programming language, the eight basic data types in it do not support object-oriented programming. The data of the basic types do not have the characteristics of "object" - no attributes and no methods to call. They are used only to cater to deeply ingrained human habits and indeed to be simple and efficient for routine data processing.

This approach of relying on non-object-oriented technology sometimes brings inconvenience. For example, the reference type data inherits the characteristics of the Object class. To convert to String type (often there is such a need), you only need to simply call the method defined in the Object class. toString() is enough, but converting primitive data types to String types is much more troublesome. In order to solve such problems, Java has designed corresponding classes for each basic data type, which are called Wrapper Classes, and there are also textbooks called Wrapper Classes or Data Type Classes.

Basic data types and corresponding wrapper classes
basic data type Corresponding wrapper class
byte Byte
short Short
int Integer
long Long
char Character
float Float
double Double
boolean Boolean


Objects of each wrapper class can encapsulate a corresponding primitive type of data and provide other useful methods. Once the wrapper class object is created, its content (the encapsulated primitive type data value) cannot be changed.

-------------------------------------------------------------------------------------------------------------

 

Boxing: Convert basic data types into corresponding packaging objects.

Unboxing: Convert the wrapper class object into the corresponding basic data type.

Autoboxing: You can assign a basic type variable directly to the corresponding wrapper class variable.

Automatic unboxing: Allows the wrapper class object to be directly assigned to the corresponding basic data type variable.

 

Automatic boxing and unboxing is a new feature at the compiler level, and manual boxing and unboxing is still the case at the bottom.

-----------------------------------------------------------------------------------------------------------

 

The data types supported by the switch statement: byte, short, chart, int also support the corresponding wrapper classes. Why?

  Because at the bottom, switch will manually unbox the wrapper class.

---------------------------------------------------------------------

 

Object can accept values ​​of all data types.

 

Reference: https://www.cnblogs.com/ok932343846/p/6749488.html

--------------------------------------------------------------------------------------------------

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325206551&siteId=291194637