Java basics-conversion between basic types and packaging classes, between basic types and strings, and between java strings

Source: Internet
Author: User

Java basics-conversion between basic types and packaging classes, between basic types and strings, and between java strings
Conversion between the basic type and the packaging class

The conversion between the basic type and the packaging class is often required. Take Integer as an example (the operations of other packaging classes are the same ):

After the automatic packing and unpacking mechanism is introduced in JDK1.5, the conversion between the packaging class and the basic type is easier and more convenient.

So what is packing and unpacking? Let's take a look at them separately.

Packing:Converts a basic type to a packaging class so that it has the object nature. It can also be divided into manual packing and automatic packing.

Unpack:In contrast to packing, you can convert a packaging object to a value of the basic type, which can be divided into manual unpacking and automatic unpacking.

 

In actual conversion, use the constructor of the Integer class and the intValue method inside the Integer class to convert these types. The implementation code is as follows:

Int n = 10; Integer in = new Integer (100); // convert int type to Integer type Integer in1 = new Integer (n ); // convert Integer-type objects to int-type int m = in. intValue ();

A new feature of JDK5.0 is automatic packing and automatic unpacking.

Automatic Packing
-The basic type is automatically encapsulated into packages of similar types, such as Integer I = 100;
-In essence, the compiler automatically adds: Integer I = new Integer (100) during compilation );

Automatic unpacking
-The packaging object is automatically converted to the basic data type. For example, int a = new Integer (100 );

-In essence, the compiler automatically adds: int a = new Integer (100). intValue () during compilation ();

 

Extended article: getting started with JAVA cainiao-common packaging classes for Java basic data types (26)

Conversion between basic types and strings

In program development, we often need to convert between the basic data type and string.

Where,Convert basic type to stringThere are three methods:

1. Use the toString () method of the packaging class

2. Use the valueOf () method of the String class

3. Add the basic type with an empty string to obtain the string corresponding to the basic data type.

Let's take a lookConvert string to basic typeThere are two methods:

1. Call the parseXxx static method of the packaging class

2. Call the valueOf () method of the packaging class to convert it to the basic type of the packaging class, and the package will be automatically split.

PS: Conversion of other basic types and strings is not listed here. The methods are similar.

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.