Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:

>> GET ACCESS NOW

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's 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, let's have a quick look at 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.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!