Box & Unbox

Source: Internet
Author: User

The object cannot be forcibly converted to integer.

The object can be forcibly converted to a string.

Integer A = integer. parseint (string Str)

 

Packing and unpacking? Is the unpacking automatic?

The eight basic types are not corresponding to the system. You cannot call methods such as tostring (), hashcode (), getclass (), and equals () as objects.

For the eight basic types, qigong provides packaging for each basic type.

Int ---- integer

Char --- character

Float ---- float

Double --- double

Byte ---- byte

Short ---- short

Long --- long

Boolean --- Boolean

Box is the basic type corresponding to them, so that they have the characteristics of objects.

In the opposite direction of the Unbox, the object of the reference type is simplified to the value type.

Before jdk1.5, You need to manually box and Unbox

Jdk1.5 is automatically completed.

 

In the wrapper packaging class, there is an important static method parse. You can convert the string type to the corresponding basic data type. If the value in the string cannot be converted to the basic data type, Java is thrown. lang. numberformatexception

 

Public class parseint {

 

/**

* @ Param ARGs

*/

Public static void main (string [] ARGs ){

// Todo auto-generated method stub

 

Int p = 1;

Integer q = new INTEGER (p); // manually boxed

Int M = Q. intvalue (); // manually unpack

System. Out. println ("manual box q =" + q );

System. Out. println ("manual Unbox M =" + M );

Int J = 2;

Integer A = J;

Int B =;

System. Out. println ("auto Box A =" + );

System. Out. println ("auto Unbox B =" + B );

String STR = new string ("30i ");

Try

{

Int pp = integer. parseint (STR );

System. Out. println (PP );

}

Catch (exception E)

{

E. printstacktrace ();

}

}

 

}

 

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.