When working with the Java Number class, we typically use primitive data types such as byte, int, long, double, ...
To delve deeper into the Java Number class, you can refer to the following examples:
Integer i = 5000;
Float gpa = 13.65;
Double mask = 0xaf;
However, during development, we also encounter cases where we need to use objects instead of primitive data types. To achieve this, Java provides wrapper classes.
All Wrapper classes (Integer, Long, Byte, Double, Float, Short) are subclasses of the abstract Number class.
The object of a wrapper class contains or ends with its primitive data type. Converting primitive data into objects in Java is called boxing, and this conversion is done by the compiler. So when using a wrapper class, you only need to convert the value of the primitive data type into the constructor for the Wrapper class.
And the wrapper object will be converted back to the primitive data type, and this process is called unboxing. The Number class is part of the java.lang package.
Below are examples of boxing and unboxing in Java:
In the example above, the output result returned is: 15.
When x is assigned an integer value, the compiler 'boxes' this integer value because x is an Integer object. Then x is 'unboxed' to be able to add integers.
Methods of the Number class in Java
In the above section, Mytour just introduced to you the Number class in Java. Below is a table listing the methods that all subclasses of the Number class implement:
The recent Mytour article introduced you to the Java Number class along with a table of methods implemented by all subclasses of the Number class. It's evident that the difference between Java and C# is quite significant. If you're considering learning either of these languages, be prepared to invest a considerable amount of time in understanding them.
