Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4536

Guide to the Number Class in Java

$
0
0

1. Overview

In this tutorial, we'll discuss Java's Number class. First, we'll learn what the Number class does and what methods it contains. Then, we'll dive into the various implementations of this abstract class.

2. Number Class

Number is an abstract class in the java.lang package. Various subclasses extend the Number class. The most commonly used are:

  • Byte
  • Short
  • Integer
  • Long
  • Double
  • Float

The main purpose of this class is to provide methods to convert the numeric value in question to various primitive types such as byte, short, int, long, double, and float.

Four abstract methods are available to help accomplish the task:

  • intValue()
  • longValue()
  • doubleValue()
  • floatValue()

Number also has two concrete methods, byteValue() and shortValue(), which will return the byte value and short value of a specified number, respectively. To learn more about the different implementations of the Number class, please refer to our article on Wrapper Classes.

In the upcoming sections, we'll learn more about these methods and their usage.

3. Concrete Methods

Let's discuss the concrete methods one by one.

3.1. shortValue()

As the name suggests, this method converts the specified Number object into a primitive short value. The default implementation casts the int value into short and returns it. However, subclasses have their own implementations, and they cast respective values into short and then return.

Here is how a Double value is converted into a short primitive type:

@Test
public void givenDoubleValue_whenShortValueUsed_thenShortValueReturned() {
    Double doubleValue = Double.valueOf(9999.999);
    assertEquals(9999, doubleValue.shortValue());
}

3.2. byteValue()

This method returns the value of the specified Number object as a byte value. Nevertheless, child classes of the Number class have their own implementations. Here is how a Float value can be converted into a byte value:

@Test
public void givenFloatValue_whenByteValueUsed_thenByteValueReturned() {
    Float floatValue = Float.valueOf(101.99F);
    assertEquals(101, floatValue.byteValue());
}

4. Abstract Methods

Additionally, the Number class also has a few abstract methods and several subclasses that implement them. In this section, we'll see how these methods are used.

4.1. intValue()

This method returns the int representation of the Number in context. Let's see how a Long value can be changed into int:

@Test
public void givenLongValue_whenInitValueUsed_thenInitValueReturned() {
    Long longValue = Long.valueOf(1000L);
    assertEquals(1000, longValue.intValue());
}

Certainly, the compiler is performing a narrowing operation here by converting a long value into an int.

4.2. longValue()

This method will return the value of the Number specified as a long. In this example, we see how an Integer value is converted into a long via the Integer class:

@Test
public void givenIntegerValue_whenLongValueUsed_thenLongValueReturned() {
    Integer integerValue = Integer.valueOf(100);
    assertEquals(100, integerValue.longValue());
}

In contrast to the intValue() method, longValue() returns the long value after a widening primitive conversion.

4.3. floatValue()

We can use this method to return the value of the specified Number as a float. Let's take a look at how a Short value can be converted into a float value:

@Test
public void givenShortValue_whenFloatValueUsed_thenFloatValueReturned() {
    Short shortValue = Short.valueOf(127);
    assertEquals(127.0F, shortValue.floatValue(), 0);
}

Likewise, longValue() and floatValue() also perform a widening primitive conversion.

4.4. doubleValue()

Finally, this method converts the value of the given Number class to the double primitive data type and returns it. Here is an example of using this method to convert a Byte to double:

@Test
public void givenByteValue_whenDoubleValueUsed_thenDoubleValueReturned() {
    Byte byteValue = Byte.valueOf(120);
    assertEquals(120.0, byteValue.doubleValue(), 0);
}

5. Conclusion

In this quick tutorial, we took a look at some of the most important methods in the Number class. Finally, we've demonstrated how these methods can be used in various Wrapper classes.

As always, the full source code of the article is available over on GitHub.


Viewing all articles
Browse latest Browse all 4536

Trending Articles