Java Number Class (wrapper class for data types)

Source: Internet
Author: User

Java number

Generally, when you need to use numbers, we usually use built-in data types, such as: Byte, int, long, double, and so on.
For example:

    int i = 5000;    float gpa = 13.65;    byte mask = 0xaf;
    • 1
    • 2
    • 3
    • 1
    • 2
    • 3

However, in the actual development process, we often encounter situations where objects need to be used rather than built-in data types. To solve this problem, the Java language provides the corresponding wrapper class for each built-in data type.
All wrapper classes (Integer, Long, Byte, Double, Float, short) are subclasses of the abstract class number.

This kind of wrapper, specifically supported by the compiler, is called boxing , so when the built-in data type is used as an object, the compiler will box the built-in type into a wrapper class. Similarly, the compiler can also disassemble an object into a built-in type. The number class belongs to the Java.lang package.
Here is an example of packing and unpacking:

    public class Test{       public static void main(String args[]){ Integer x = 5; // boxes int to an Integer object x = x + 10; // unboxes the Integer to a int System.out.println(x); } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

The results of the operation are as follows:
15

When x is assigned an integer value, because X is an object, the compiler is boxing the X. Then, to allow X to be added, the X is disassembled.

To allow X to be added, the X is disassembled.

To allow X to be added, the X is disassembled.

To allow X to be added, the X is disassembled.

To allow X to be added, the X is disassembled.

To allow X to be added, the X is disassembled.

To allow X to be added, the X is disassembled.

Java Number Class (wrapper class for data types)

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.