Getting started with JavaSE 30: Packaging of common Java classes

Source: Internet
Author: User

Getting started with JavaSE 30: Packaging of common Java classes
1. Packaging

We are very familiar with basic data types, such as int, float, double, boolean, and char. The basic data type is not characteristic of objects.

For example, the basic type cannot call methods, and the function is simple. To make basic data types have the object features, Java provides

A packaging class is provided, so that we can operate on the basic data type like the operation object.

Packaging classes (such as Integer and Double) encapsulate a corresponding basic data type value and provide a series of operations for it.

Mappings between basic types and packaging classes:

Packaging provides two main methods:

1. Method for converting this type and other basic types;

2. Convert the string, the current type, and the packaging class.

Binary Integer Packaging

Let's take the java. lang. Integer packaging class as an example to look at the characteristics of the packaging class.

The construction method of Integer packaging class:

The following code is used:

Common Methods of Integer packaging:

Instance:

Public class Test {public static void main (String args []) {// defines int type variables with a value of 86 int score1 = 86; // creates an Integer packaging class object, the value of score1 is Integer score2 = new Integer (score1); // converts the Integer packaging class to double type double score3 = score2.doubleValue (); // convert the Integer packaging class to the float Type float score4 = score2.floatValue (); // convert the Integer packaging class to the int type int score5 = score2.intValue (); // output the result System. out. println ("Integer packaging class:" + score2); System. out. println ("double Type:" + score3); System. out. println ("float Type:" + score4); System. out. println ("int type:" + score5 );}}

Compile the running result:

Conversion between three primitive types and packages

The conversion between the basic type and the packaging class is often required, or the Integer packaging class is used as an example to explain, the usage of other packaging classes is basically similar.

See the following code example:

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?

What about it? Let's take a look at the following:

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

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

Instance:

Public class Test {public static void main (String args []) {// defines the double Type Variable double a = 91.5; // manually boxed Double B = new Double (); // Double c = a; System. out. println ("the result after packing is:" + B + "and" + c); // defines a Double packaging class object with a value of 8 Double d = new Double (87.0 ); // manually unpack double e = d. doubleValue (); // automatically unpack double f = d; System. out. println ("the result after unpacking is:" + e + "and" + f );}}

Compile the running result:

4. Conversion between basic types and strings

In program development, we often need to convert between the basic data type and string. There are three methods to convert basic types into strings:

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.

To convert a string to a basic type, you can use either of the following 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 is automatically split.

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

Instance:

 

Public class Test {public static void main (String args []) {double m = 78.5; // convert the basic type to String str1 = Double. toString (m); System. out. println ("the result of summation with integer 20 after m is converted to String type:" + (str1 + 20); String str = "180.20 "; // convert the string to the basic type Double a = Double. valueOf (str); System. out. println ("the result of summation with integer 20 after str is converted to double type is:" + (a + 20 ));}}

 

Compile the running result:

Summary of five packaging categories

(1) All packaging classes (Integer, Long, Byte, Double, Float, and Short) are subclasses of the abstract class Number.

Member methods of the Number class:

(2) Character class

1 escape sequence

The character with a backslash (\) represents the escape character, which has a special meaning for the compiler. The following lists the escape sequences of Java:

2Character class member method:

 

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.