Java Basics: Wrapper classes for basic data types in Java (primarily for easier conversion between different data types)

Source: Internet
Author: User

I believe that all of you are very familiar with the basic data types, such as int, float, double, Boolean, Char, and so on. Basic data types do not have the properties of the object, such as the basic type cannot invoke the method, the function is simple ... , Java provides a wrapper class for each base data type, so that we can manipulate the base data type as an operand, in order for the basic data type to have the properties of the object.

Correspondence between the base type and the wrapper class:

The wrapper class provides two main types of methods:

1. Ways to convert this type and other basic types

2. Ways to convert strings and this type and wrapper class to each other

We will walk through the steps in the following sections, the table anxious Oh ~ ~

Task

We take the Integer wrapper class as an example to see the characteristics of the wrapper class.

How to construct the Integer wrapper class:

As shown in the following code:

Common methods for integer wrapper classes:

The following program lists the conversion between the Integer type and the basic data type, and first recognize it with the results of the operation.

1  Public classHelloWorld {2      Public Static voidMain (string[] args) {3         4         //defines an int type variable with a value of5         intScore1 = 86; 6         7         //Create an Integer wrapper class object that represents the value of the variable score18Integer score2=NewInteger (score1);9         Ten         //converting an Integer wrapper class to a double type One         Doublescore3=Score2.doublevalue (); A          -         //converting an Integer wrapper class to a float type -         floatscore4=Score2.floatvalue (); the          -         //converting an Integer wrapper class to an int type -         intScore5 =Score2.intvalue (); -  +System.out.println ("Integer wrapper class:" +score2); -System.out.println ("Double type:" +score3); +System.out.println ("Float type:" +Score4); ASYSTEM.OUT.PRINTLN ("int type:" +score5); at     } -}

Java Basics: Wrapper classes for basic data types in Java (primarily for easier conversion between different data types)

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.