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;
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); } }
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)