Home  >  Article  >  Java  >  Detailed explanation of java packaging class

Detailed explanation of java packaging class

王林
王林forward
2020-07-08 16:34:483394browse

Detailed explanation of java packaging class

1. Definition:

(Recommended learning: java entry program)

Java is An object-oriented programming language, but the eight basic data types in Java are not object-oriented. In order to facilitate use and solve this shortcoming, a corresponding class is designed to represent each basic data type when designing the class, so that The classes corresponding to the eight basic data types are collectively called wrapper classes, and wrapper classes are located in the java.lang package.

2. Purpose of packaging classes

For ease of use, Java encapsulates 8 basic data types: In addition to the Integer and Character classes, the other six The class name of the class must be consistent with the basic data type, as long as the first letter of the class name is capitalized.

boolean —> Boolean

char —> Character

byte—> Byte

short—> Short

long—> Long

int —> Integer

float—> Float

double—> Double

For packaging classes, use There are mainly two types:

a. They exist as classes corresponding to basic data types to facilitate operations involving objects.

b. Contains the relevant attributes of each basic data type, such as maximum value, minimum value, etc., as well as related operation methods.

3. The actual use of packaging classes (taking int and integer as an example)

1. Conversion between int and integer classes

In During actual conversion, use the constructor of the Integer class and the intValue method inside the Integer class to achieve mutual conversion between these types.

        //
        int n=5;
        Integer n1=new Integer(n);
        System.out.println("int类型转换为integer类:"+n1);
        //
        Integer i=new Integer(50);
        int i1 = i.intValue();
        System.out.println("integer类转换为int类型:"+i1);

2. Common methods inside the Integer class

The main methods of the Integer class are: parseInt method and toString method.

        //parseInt方法: 数字字符串类型转成int类型
        String ss="123";
        int ii = Integer.parseInt(ss);
        System.out.println("字符类型转成整型:"+ii);
        //toString方法:int类型转成数字字符串类型
        int ii2=123;
        String ss2 = Integer.toString(ii2);
        System.out.println("int类型转成数字字符串类型:"+ss);

JDK has introduced automatic unboxing syntax since version 1.5, that is, when converting basic data types and corresponding packaging classes, the system will automatically do so, which will greatly facilitate programmers. Code writing.

(Video tutorial recommendation: java video tutorial)

Autoboxing: Encapsulate basic data types into object types to conform to Java's object-oriented thinking.

Automatic unboxing: Reconvert the object into a basic data type.

        //5是基本数据类型,通过自动装箱变成对象类型。
        //编译器执行了Integer iii = Integer.valueOf(5)
        Integer iii=5;
        //自动拆箱,实际上执行了 int iii2 = iii.intValue()
        int iii2=iii;
        System.out.println(iii2);

Explanation: Integer iii=5; is equivalent to the compiler executing the Integer iii = Integer.valueOf(5) operation.

This method returns an Integer object, but before returning, it is regarded as a judgment to determine whether the current value of i is in the [-128,127] difference, and whether this object exists in the IntegerCache. If it exists, then Return the reference directly, otherwise, create a new object.

3. Cache value of packaging class

Cache value range of each packaging class:

boolean:true和false 
byte:-128~127 
char:0~127 
short:-128~127 
int:-128~127 
long:-128~127

Special note: There is no cache for float and double.

When boxing the above values ​​(such as: Character c1=127; or Integer i= Integer.valueOf(1); This will not work: Integer i2=new Integer(1); Because this is creating a new object.), it does not create a new object but uses the object in the cache. If it exceeds the scope, a new object needs to be created.

The above is the detailed content of Detailed explanation of java packaging class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete