The java.lang.Number class is an abstract class that represents all numeric types in Java. It is the superclass of the following classes:
- BigDecimal: The BigDecimal class represents a floating-point number with arbitrary precision. This means that it can represent numbers with a very large or very small magnitude, without rounding or truncation. BigDecimal objects are immutable. Meaning, these value can’t not be changed once created.
- BigInteger: The BigInteger class represents an integer with arbitrary precision. This means that it can represent integers that are much larger than the maximum value of a primitive int or long.
- Byte: The Byte class represents a byte value, which is a signed 8-bit integer. The range of values that a Byte object can represent is from -128 to 127.
- Double: The Double class represents a double-precision floating-point number. We can use Double objects to represent real numbers.
- Float: The Float class represents a single-precision floating-point number. We can also use Float objects to represent real numbers that do not need to be as precise as double numbers.
- Integer: The Integer class represents an int value, which is a signed 32-bit integer. The range of values that an Integer object can represent is from -2147483648 to 2147483647.
- Long: The Long class represents a long value, which is a signed 64-bit integer. The range of values that a Long object can represent is from -9223372036854775808 to 9223372036854775807.
- Short: The Short class represents a short value, which is a signed 16-bit integer. The range of values that a Short object can represent is from -32768 to 32767.
The Number class provides a number of methods for working with numeric values, such as:
Method | Description |
---|---|
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. |
byteValue() | Returns the value of the Number object as a byte. |
toString() | Returns a string representation of the Number object. |
compareTo() | Compares the value of the Number object to the value of another Number object. |
equals() | Compares the value of the Number object to the value of another object. |
hashCode() | Returns a hash code for the Number object. |
The Number class is also used to implement the Comparable and Serializable interfaces. So that we can compare the Number objects to each other and serialize to and from a stream.
Why use Number class?
There are a few reasons why we might want to use the Number class instead of working with primitive numeric types directly.
- Object-oriented programming: The Number class provides a more object-oriented way to work with numbers. This can be helpful for code that needs to be more modular and reusable.
- Type safety: The Number class ensures that we are working with a valid numeric type. This can help to prevent errors and improve the readability of our code.
- Utility methods: The Number class provides a number of utility methods that can be helpful for working with numbers. For example, we can use the intValue() method to convert a Number object to an int value, or the toString() method to convert a Number object to a string.
How to use Number class
To use the Number class, we first need to create a Number object. We can do this by using one of the constructors that is provided by the Number class, or by casting a primitive numeric type to a Number object.
Once we create a Number object, we can use the methods that are provided by the Number class. We can use these methods to work with the numeric value that is represented by the object. For example, we can use the intValue() method to get the int value represented by the Number object, or the toString() method to get a string representation of the Number object.
Example
The following code shows an example of how to use the Number class:
public class Example {
public static void main(String[] args) {
// Create a Number object
Number number = new Integer(10);
// Get the int value of the Number object
int value = number.intValue();
// Convert the Number object to a string
String string = number.toString();
// Print the value and string representations of the Number object
System.out.println("The value of the Number object is: " + value);
System.out.println("The string representation of the Number object is: " + string);
}
}
This code will print the following output:
The value of the Number object is: 10
The string representation of the Number object is: 10
Conclusion
The java.lang.Number class is a powerful tool for working with numeric values in Java. It provides a number of methods that we can use to convert, compare, and manipulate numeric values. If we are working with numbers in Java, the Number class is a valuable resource that we should be familiar with.