Java Math nextAfter()

That is, if the first argument is 6.7 and the second argument is 2.3, the adjacent number of 6.7 in direction of 2.3 is 6.699999999999999.

The syntax of the nextAfter() method is:

Math.nextAfter(start, direction)

Note: The nextAfter() method is a static method. Hence, we can call the method directly using the class name Math.


nextAfter() Parameters

  • start - starting number whose adjacent number is returned
  • direction - specifies which adjacent number of start is to be returned

Note: The data type of start and direction can be either float or double.


nextAfter() Return Values

  • returns the number adjacent to start towards direction

Note: If the start and direction are equal, then the value equivalent to direction is returned.


Example: Java Math.nextAfter()

class Main {
  public static void main(String[] args) {

    // float arguments
    // returns the smaller adjacent number
    float start1 = 7.9f;
    float direction1 = 3.3f;
    System.out.println(Math.nextAfter(start1, direction1));  // 7.8999996

    // double arguments
    // returns the larger adjacent number
    double start2 = 7.9f;
    double direction2 = 9.8f;
    System.out.println(Math.nextAfter(start2, direction2));  // 7.9000000953674325

  }
}

Recommended Tutorials

Did you find this article helpful?