Description

On this document we will be showing a java example on how to use the nextUp() method of Math Class. The nextUp() returns the floating-point value adjacent to d in the direction of positive infinity. Make a note that the nextUp() method is overloaded which means that we have more than one method with the same name under the Math class. Below are the two overloaded method of the nextUp().

  • public static double nextUp(double d)
  • public static float nextUp(float f)

The two overloaded methods are basically the same, it’s just that they deal with different data type either float or double.

Most of the methods of the Math class is static and the nextUp() method is no exception. Thus don’t forget that in order to call this method, you don’t have to create a new object. Use the method in the format Math.nextUp(start, direction).

Notes:

  • If the argument is NaN, the result is NaN.
  • If the argument is positive infinity, the result is positive infinity.
  • If the argument is zero, the result is Double.MIN_VALUE if we are dealing with double and if it’s float then the result is Float.MIN_VALUE.

Method Syntax

public static double nextUp(double d)
public static float nextUp(float f)

Method Returns

The nextAfter() method returns the adjacent floating-point value closer to positive infinity.

Compatibility

Requires Java 1.6 and up

Java Math nextUp() Example

Below is a java code demonstrates the use of nextUp() method of Math class. The example presented might be simple however it shows the behavior of the nextUp() method.

The above java example source code demonstrates the use of nextUp() method of Math class. We simply ask for user input and we use the Scanner class to parse it. Since we have used the nextLine() method to get the console value which is having a return data type of String thus we have used the Double.parseDouble() to transform it into double. Alternatively if the requirements is to use float then you must use the Float.parseFloat() instead. This conversion is required because the argument for nextUp() method only accepts either float or double.

Sample Output

Below is the sample output when you run the above example.

java lang Math nextUp() example output