Javase Introductory Learning 30:java common classes of packaging class

Source: Internet
Author: User

a packaging class

We are very familiar with basic data types, such as int, float, double, Boolean, Char, and so on. Basic data types are attributes that do not have an object

, such as the basic type can not invoke the method, the function is simple. In order for the basic data type to have the properties of the object, Java provides each basic data type

Provides a wrapper class so that we can manipulate the basic data type as if it were an object.

wrapper classes (e.g., integer,double, etc.) these classes encapsulate a corresponding base data type value and provide it with a series of operations.

Correspondence between the base type and the wrapper class:


The wrapper class provides two main types of methods:

1 Methods for converting this type and other basic types;

2 The method of converting a string to and from this type and wrapper class to each other.

two integer wrapper class

We take the Java.lang.Integer packaging class as an example to see the characteristics of the packaging class.

How to construct the integer wrapper class:


As shown in the following code:


Common methods for integer wrapper classes:


Instance:

public class test{public    static void Main (String args[]) {                //defines an int type variable with a value of                score1 int =;         Creates an integer wrapper class object that represents the value of the variable score1 integer score2=new integer (score1);        Converts an integer wrapper class to a double type Double score3=score2.doublevalue ();        Converts the integer wrapper class to float type float score4=score2.floatvalue ();        Converts an integer wrapper class to an int type int score5 =score2.intvalue ();                Output result System.out.println ("Integer wrapper class:" + Score2); System.out.println ("Double type:" + score3); System.out.println ("Float type:" + Score4); SYSTEM.OUT.PRINTLN ("int type:" + Score5);}   }

Compile run Result:


Three conversions between basic types and wrapper classes

The basic type and wrapper class often need to convert each other, or the integer wrapper class as an example to explain, the use of other packaging classes are basically similar.

Look at the following code example:


after the introduction of the automatic boxing and unpacking mechanism in JDK1.5, the wrapper class and the base type conversion is easier and more convenient. What is the packing and unpacking?

It? Let's look at each of them:

Boxing: The basic type is converted into a wrapper class, so that it has the nature of the object, but also can be divided into manual boxing and automatic boxing.


Unpacking: In contrast to boxing, the wrapper class object is converted into a basic type of value, which can be divided into manual unpacking and automatic unpacking.


Instance:

public class test{public    static void Main (String args[]) {        //define double type variable        double A = 91.5;                 Manual boxing         Double b = new double (a);                       Auto boxing        Double c = A;              System.out.println ("Boxed results are:" + B + "and" + C);                Defines a double wrapper class object with a value of 8        double d = new double (87.0);                Manual unpacking        Double e = D.doublevalue ();                Automatic unpacking        double f = d;                SYSTEM.OUT.PRINTLN ("The result after unpacking is:" + E + "and" + f);}   }

Compile run Result:


Four conversions between basic types and strings

In program development, we often need to convert between the basic data type and the string. Where the basic type is converted to a string there are three methods:

1 using the ToString () method of the wrapper class;

2 using the valueof () method of the String class;

3 with an empty string plus the base type, you get the string that corresponds to the base type data.


Again, there are two ways to convert a string to a basic type:

1 Call the Parsexxx static method of the wrapper class;

2 The ValueOf () method that calls the wrapper class is converted to a wrapper class of the base type, and the box is automatically removed.


Other basic types and string conversions are no longer listed here, and the methods are similar.

Instance:

public class test{public    static void Main (String args[]) {        Double m = 78.5;//Converts the base type to string str1 = Double.tostr ing (m);        System.out.println ("M is converted to string and the sum of Integer 20 is:" + (STR1+20)); String str = "180.20";//Convert string to basic type Double a =  double.valueof (str); System.out.println ("The sum of the integer 20 after the STR is converted to Double is:" + (A+20));}   }

Compile run Result:


Summary of five packaging categories

(1) All wrapper classes (Integer, Long, Byte, Double, Float, short) are subclasses of the abstract class number.


Member methods for the number class:




(2) character class

1 Escape sequences

A character preceded by a backslash (\) represents an escape character, which has a special meaning for the compiler. The following list shows the escape sequences for Java:


Member methods for the 2Character class:



Javase Introductory Learning 30:java common classes of packaging 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.