Java Programming Introductory Tutorial--Original Classes and Wrapper Classes

Packaging

       The Java language is an object-oriented language, but the basic data types in Java are not object-oriented, which causes a lot of inconvenience in actual use.

       In order to solve this deficiency, a corresponding class is designed for each basic data type to represent when designing classes, so the eight classes corresponding to the basic data types are collectively called wrapper classes (Wrapper Class), and some places are also translated as encapsulation classes or data type classes .

       The wrapper classes are located in the java.lang package.


The corresponding relationship between wrapper classes and basic data types is shown in the table

basic data type

Packaging

Construction method

boolean

Boolean

Booleanboolean b)或Boolean(String s)

byte

Byte

Byte(byte b)Byte(String s)

char

Character

Character(char c)

short

Short

Short(short s ) Short(String s)

int

Integer

Integer( int i ) or Integer(String s)

long

Long

Long(long l) or Long(String s)

float

Float

Float(double d) or Float(float f) or Float(String s)

double

Double

Double(double d) or Double(String s)

        In addition, there are two wrapper classes BigInteger and BigDecimal that have no corresponding basic types and are mainly used for high-precision operations. BigInteger supports integers with arbitrary precision, and BigDecimal supports operations with arbitrary precision and decimal points.


Function of the wrapper class

        The wrapper class belongs to the reference data type and has attributes and methods. Using these methods, many functions that the basic data types do not have can be realized. The wrapper class mainly improves the following functions: (1) After wrapping the basic data type, it can be used as an object; (2) Provide various conversion functions for the basic data type, such as converting a value to a string and converting a string to a value .

        Some data structures such as ArrayList, HashMap , etc. cannot store the original value type, and only its wrapper class can be used at this time.

Conversion between basic data types and their wrapper classes (take the Integer class as an example)

(1) Convert the basic type to the package type: Integer I=new Integer(primitive value);

(2) The wrapper type is converted to the basic type: int a= I.intValue () .

The main similarities and differences between basic types and packaging types:
1.  In Java , everything is an object, but the eight basic types are not objects.

2.  Different declaration methods, the basic type does not need to be created through the new keyword, while the package type needs the new keyword.

3.  The storage method and location are different. The basic type is to directly store the value of the variable and store it in the stack for efficient access. The encapsulation type needs to point to the instance by reference, and the specific instance is stored in the heap.

4.  The initial value is different. The initial value of the encapsulation type is null , and the initial value of the basic type depends on the specific type. For example, the initial value of the int type is 0 , and the boolean type is false ;

5.  Different ways of use, for example, only packaging types can be used when used in cooperation with collection classes.

 

Guess you like

Origin blog.csdn.net/u010764893/article/details/131022924
Recommended