Javatpoint Logo
Javatpoint Logo

Java Number Class

Java provides various classes and tools to manage different data kinds and processes. The Number class acts as a superclass for Java's numeric wrapper classes, is one example of a fundamental class. It contains methods to convert, compare, and perform arithmetic operations on various numeric types and a basis for working with numerical values. In this section, we'll detail the Number class, its subclasses, and how it's used in Java programming.

The Number class belongs to java.lang package. It is an abstract superclass for classes that wrap numbers, including Byte, Short, Integer, Long, Float, and Double. It offers methods for number comparison, arithmetic operations, and converting numerical values to their respective primitive types. The number is an abstract class that cannot be constructed directly but offers its subclasses a common interface and functionality.

The Subclasses of Number

The Number class is expanded by numerous numeric wrapper classes, as was previously described. Let's talk about each of these subclasses briefly:

  1. Byte: The Byte class allows for manipulating a signed 8-bit integer by encapsulating a byte value. It offers ways to execute arithmetic operations, transform bytes into other primitive kinds, and parse byte values from strings.
  2. Short: A 16-bit signed integer is represented by the Short class. It has methods for parsing, converting, and doing arithmetic on short values comparable to the Byte class.
  3. The integer class is one of the most often used numeric wrapper classes. It supports a variety of techniques for mathematical operations, parsing, and conversion between integer values and other data types and represents a 32-bit signed integer.
  4. Long: The 64-bit signed integers are the focus of the Long class. It offers techniques for working with lengthy values, including conversion, parsing, and arithmetic operations.
  5. 32-bit floating-point values with single precision are included in the Float class. It provides ways to compare floats, perform arithmetic operations, and transform float values into other data types.
  6. Double: The Double class represents a double-precision 64-bit floating-point value. Though with more accuracy, it offers Float class-like capabilities.

Common Methods of the Number Class

The Number class exposes several methods shared by all of its subclasses despite being abstract and unable to be instantiated. Let's examine a few of the often-employed techniques:

  • ShortValue(), IntValue(), LongValue(), FloatValue(), and DoubleValue() These methods yield the type's corresponding numeric value. For instance, the function intValue() returns the number's integer value.
  • The toString() function represents the numerical value as a String.
  • equals(Object obj): This method evaluates if the current number and the given object are equal.
  • Compare two numbers' values using the method compareTo(Number another number). A positive value is returned if the current number is larger than the argument; otherwise, it returns a negative value if the current number is less than the argument.

Autoboxing and Unboxing

This is one of the primary characteristics of all numeric wrapper classes, even those that extend the Number class. While unpacking is the automated conversion of wrapper class objects to their corresponding primitive types, autoboxing is the automatic conversion of primitive types to their corresponding wrapper classes. For instance, autoboxing enables the assignment of an int value to an Integer object without explicit conversion.

Arithmetic Operations

Arithmetic operations can be carried out on numerical values using methods provided by the Number class. These methods, nevertheless, are abstract, and each subclass has a unique implementation of them. For instance, the Number class defines the add(), subtract(), multiply(), and division() methods, but Integer, Double, and other subclasses override them to handle the different data types properly.

It is crucial to remember that arithmetic operations on the Number class and its subclasses return the outcome as an object of the relevant subclass. Consider utilizing the BigDecimal class, which is not a subclass of Number but offers additional control over decimal arithmetic if you need to execute math with precise accuracy or rounding.

Parsing and Conversion

The Number class and its subclasses include methods for parsing texts and changing numeric values into other data types. These techniques come in handy when you need to transform data from external sources or user input into the right numerical representations.

The wrapper classes' parseXxx() methods, which include parseInt(), parseDouble(), and others, enable transforming textual representations of integers into the relevant data types.

Comparison and Equality

The Number class offers ways to compare and ensure that numerical values are equal. Depending on their relative order, the compareTo() function compares two numbers and provides a result that is either negative, zero, or positive. The equals() function examines the equality of two integers.

We may create logic based on the comparison findings by using these methods to conduct comparisons and equality tests between numeric numbers.

Performance Issues

Although the numeric wrapper classes are convenient, knowing how they may affect performance is vital. Unlike primitive types, wrapper class objects have more memory overhead, which might result in autoboxing and unpacking costs in some cases. Consider utilizing primitive types directly when working with huge volumes of numerical data or performance-sensitive programming.

Example for Usage of the Number Class

Consider creating a financial application that does financial computations. You might have to complete formatting, rounding, and currency conversion tasks. In certain situations, the Number class and its subclasses can greatly assist. Here is an illustration of the usage:

FinancialApplication.java

Output:

Amount in USD: $1000.5
Amount in EUR: EUR 1,150.57

Explanation

In the aforementioned illustration, a financial application uses an exchange rate to convert a specified amount of USD to EUR. To represent the quantity, we utilize the double data type. The Number class and its subclasses are used as follows:

The monetary quantities are represented by the double values amountInUSD and exchangeRate, which are instances of the Double class, a subclass of Number.

The amountInUSD is multiplied by the exchangeRate to get the amountInEUR, which is once more a double value, and this is how we convert currencies.

Number Class Methods

Java Number class provides methods to convert the represented numeric value to byte, double, float, int, long, and short type. The various Java Number methods are as follows-

SN Modifier & Type Method Description
1) Byte byteValue() It converts the given number into a byte type and returns the value of the specified number as a byte.
2) abstract double doubleValue() It returns the value of the specified number as a double equivalent.
3) abstract float floatValue() It returns the float equivalent value of the specified Number object.
4) abstract int intValue() It returns the value of the specified number as an int.
5) abstract long longValue() It returns the value of the specified number object as long equivalent.
6) short shortValue() It returns the value of the specified number as a short type after a primitive conversion.

Conclusion

In conclusion, the Java Number class and its subclasses offer a practical method for dealing with numerical quantities. The Number class provides a consistent interface and functionality regardless of whether you must compare, convert, or perform arithmetic operations on distinct numeric types. You can develop reliable code to handle numeric data in your Java applications by understanding the capabilities and methods of the Number class.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA