Number Class
The Number class is a wrapper class in Java that encapsulates primitive numeric types such as byte, short, int, long, float, and double as objects. It provides a number of useful methods to perform various operations on numeric values. Here are some common methods of the Number class:
byteValue(): Returns the value of the Number object as a byte.
shortValue(): Returns the value of the Number object as a short.
intValue(): Returns the value of the Number object as an int.
longValue(): Returns the value of the Number object as a long.
floatValue(): Returns the value of the Number object as a float.
doubleValue(): Returns the value of the Number object as a double.
equals(Object obj): Compares the Number object with the specified object for equality.
toString(): Returns a string representation of the Number object.
hashCode(): Returns the hash code value for the Number object.
compareTo(Number anotherNumber): Compares the Number object with another Number object.
isNaN(): Returns true if the value of the Number object is NaN (not a number).
isInfinite(): Returns true if the value of the Number object is infinite.
Example use the Number class in Java
refer to:theitroad.compublic class NumberExample { public static void main(String[] args) { // Create a Number object Number num = 123.45; System.out.println("Number is: " + num); // Convert the Number object to an integer int intValue = num.intValue(); System.out.println("Integer value is: " + intValue); // Convert the Number object to a double double doubleValue = num.doubleValue(); System.out.println("Double value is: " + doubleValue); // Check if the Number object is an instance of a particular class boolean isInstance = num instanceof Double; System.out.println("Is instance of Double? " + isInstance); } }