Java learns 26 from scratch (Packaging class), and java starts packaging from scratch

Source: Internet
Author: User

Java learns 26 from scratch (Packaging class), and java starts packaging from scratch
I. Packaging

A packaging class encapsulates a basic type into a class. That is, the basic data type is encapsulated into a class type.

Java programming provides a packaging class for each basic type. These packaging classes are in the java. lang package. There are 8 packaging classes.

Ii. constructor and static methods of the packaging Class 2.1, first

Public Type (type value) the Type in the upper-case letter indicates the packaging class, and the type in lower-case indicates the basic Type.

This constructor receives a basic data type value and creates a corresponding packaging class.

You can use the new keyword to encapsulate a basic 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 constructor of the packaging class. If the value of the basic type encapsulated by the packaging class is

2.2. Second

Public Type (String value)

Converts string parameters to a packaging class, except for the Character class

Byte byteValue=new Byte("21");Float floatValue=new Float("21");
Boolean booleanValues=new Boolean("true");

The input string must meet the basic type requirements.

For example, the following error code is used:

Float floatValue=new Float("abc");
2.3. Category 3

Public static Type valueOf (type value)

Short shorValue=Short.valuesOf((short) 21);Integer intValue=Integer.valuesOf(21);Character charValue=Character.valueOf('x');Boolean booleanValue=Boolean.valueOf(true);

Public static Type valueOf (String s)

Byte byteValue=Byte.valueOf("21");Integer intValue=Integer.valueOf("21");
Iii. Conversion of common types 3.1 and conversion of packaging classes to Basic Types

Packaging class. Value (); Method

     Integer intValue=Integer.valueOf(21);        int value=intValue.intValue();        Boolean booleanValue=Boolean.valueOf("true");        boolean bvalue=booleanValue.booleanValue();
3.2 convert string to basic type

Public static typeParseType(String type)

      int num=Integer.parseInt("21");        boolean flag=Boolean.parseBoolean("true");
3.3 convert basic types to strings

Public static StringToString(Type value)

String id = Integer. toString (21); String sex = Character. toString ('male ');

Simple Writing

Use after basic type+ "" For conversion

String id = 21 + ""; String sex = 'male' + "";
3.4 Automatic packing and unpacking

Packing:Convert the basic type to the packaging Class Object

Unpack:The value of the packaging Class Object to the basic type.

After java se5.0, there is no need to use encoding to implement the conversion between them. After JDK, it will automatically help us complete the transformation.

Integer export bject = 5; // boxed int intValue = export bject; // unpack

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.