Current location - Trademark Inquiry Complete Network - Trademark inquiry - Why is there a wrapper class in JAVA and what is its function?
Why is there a wrapper class in JAVA and what is its function?
First, not all conversions between basic data types can be stopped. If there is a forced conversion, there will be problems, such as the conversion from String type to int type, so jdk provides corresponding wrapper classes for the convenience of users.

Example:

Common class integer {

private int I;

Common integer (int a) (

I = a;

}

public static int parseToInt(){

Return I;

}

Public static integer value of (string str)

//Encapsulate a series of logics to finally convert str into IntegerStr of type int.

Returns a new integer str;

}

}

The above is an example of jdk about Integer, such as Integer Intg = Integer. value of(str); int I = intg . parse toint();

In this way, users can easily complete the conversion between String and int, which is convenient for users.

Second, sometimes a function needs to pass an object variable, but you obviously can't pass in an int type. What should I do? I use a wrapper class.

Common void test (object object)

}

You can put 5 pounds in if you want.

Test (new integer (5));

To sum up, wrapper classes have three purposes: one is to realize the conversion between basic types, the other is to facilitate the function to pass values, and the third is to facilitate the replacement of basic data types when using Object in some places.