Java Starts from scratch 26 (wrapper class)

Source: Internet
Author: User

First, Packaging class

A wrapper class encapsulates a base type into a class. That is, wrapping the base data type as a class type.

Java programming provides a wrapper class for each of the basic types. These packaging classes are in the Java.lang package. There are 8 packaging classes

Second, the packing class construction method and static method 2.1, the first kind

Public type (type value) where the first letter of the uppercase type represents the wrapper class, and the lowercase type represents the base type

This construction method receives a basic data type value and creates a wrapper class corresponding to it.

You can use the New keyword to wrap a base type as an object

Integer intvalue=new Integer (21);
Long longvalue=New Long (21L);
Character charvalues=New Character (' x ');
Boolean booleanvalue=New Boolean (true);

The parameter passed to the wrapper class construction method, if the value of the base type wrapped by the wrapper class

2.2. The second type

Public Type (String value)

Converts a string argument to a wrapper class, except for the character class

BYTE bytevalue=new byte ("+"); float Floatvalue=new float ("21");
Boolean booleanvalues=new Boolean ("true");

Incoming strings conform to basic type requirements

Such as: The following incorrect wording

Float floatvalue=new Float ("abc");
2.3. The Third Kind

public static type ValueOf (type value)

Short Shorvalue=short.valuesof ((short)); Integer intvalue=integer.valuesof (+); Character charvalue=character.valueof (' x '); Boolean booleanvalue=boolean.valueof (true);

public static Type ValueOf (String s)

Byte bytevalue=byte.valueof ("+"); Integer intvalue=integer.valueof ("21");
Iii. Common Type Conversion 3.1, wrapper class conversion to basic type

Packaging class. Value (); method

Integer intvalue=integer.valueof (+);         int value=intvalue.intvalue ();        Boolean booleanvalue=boolean.valueof ("true");         boolean bvalue=booleanvalue.booleanvalue ();
3.2. Convert string to basic type

public static type parsetype(String type)

   int num=integer.parseint ("+");         boolean Flag=boolean.parseboolean ("true");
3.3. Convert basic type to string

public static String toString(type value)

String id=integer.tostring (+);        String sex=character.tostring (' Male ');

Simple notation

Use + "" after base type to convert

String ID =21+ ""; String sex= ' male ' + ';
3.4. Automatic box packing and unpacking

Boxing: the base type is converted to the wrapper class object

Unboxing: wrapper class object converted to a value of base type

After Java se5.0 does not need to use the code to implement the conversion between them, the JDK automatically helps us to complete the

Integer intobject=5;  // Packing int  Intvalue=intobject;   // Unpacking

Java Starts from scratch 26 (wrapper class)

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.