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).


  • 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.


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.


import java.util.Scanner;

 * This example source code demonstrates the use of  
 * nextUp() method of Math class

public class MathNexUpExample {

	public static void main(String[] args) {
		// Ask for user input
		// use scanner to read the console input
		Scanner scan = new Scanner(;
		// Assign the input to String variable
		String start = scan.nextLine();
		// close the scanner object
		// convert the values to double
		double doubleStart = Double.parseDouble(start);
		// get the result of nextUp() method
		double result = Math.nextUp(doubleStart);
		System.out.println("Result of the operation:"+result);


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