The role of the Java wrapper class

Source: Internet
Author: User

Reprinted from Http://zhidao.baidu.com/question/2052192149152534987.html

First, the conversion between the basic data types is not all convertible, and you cast problems, such as the conversion of string type int type, then the JDK for the convenience of users to provide the appropriate wrapper class.
Example:
public class integer{
private int i;
Public Integer (int a) {
I =a;
}

public static int Parsetoint () {
return i;
}
public static Integer valueOf (String str) {
Encapsulates a series of logic that eventually converts STR to an int type of INTEGERSTR
return new Integer (INTEGERSTR);
}
}

Above is an example of a JDK for integers such as Integer INTG = integer.valueof (str); int i = Intg.parsetoint ();
This makes it easy for the user to complete the conversion of the string and int so that the user

Second, sometimes a function needs to pass a variable of object and you want to pass int type to go in obviously not, how to do it, use the wrapper class.
public void Test (Object obj) {
}
You want to pass 5 in, you can do that.
Test (new Integer (5));

In summary, there are three uses of the packaging class

A transformation between the implementation base types

The second is to facilitate the function transfer value

Third, in some places to use the object when it is convenient to replace the basic data type

The role of the Java wrapper class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.