Java Math nextAfter() method with Example Last Updated : 30 May, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report The java.lang.Math.nextAfter() returns the floating-point number adjacent to the first argument in the direction of the second argument. If both arguments are equal then the second argument is returned. Syntax of nextAfter() method// datatype can be float or double. public static dataType nextAfter(dataType st, dataType dir) Parameter : st : starting floating-point value. dir :value indicating which of start's neighbors or start should be returned. Return: This method returns the floating-point number adjacent to the start in the direction of the direction. Note : If one of the arguments is NaN, Output is NaN If both arguments are signed zeros, direction is returned unchanged(as implied by the requirement of returning the second argument if the arguments compare as equal).If start is Double.MIN_VALUE or Float.MIN_VALUE and direction has a value such that the result should have a smaller magnitude, then a zero with the same sign as start is returned.If start is infinite and direction has a value such that the result should have a smaller magnitude, Double.MAX_VALUE or Float.MAX_VALUE with the same sign as start is returned.If start is equal to Double.MAX_VALUE or Float.MAX_VALUE and direction has a value such that the result should have a larger magnitude, an infinity with same sign as start is returned.Example of Java Math nextAfter() methodExample 1: To show the working of java.lang.Math.nextAfter() method. Java // Java program to demonstrate working // of java.lang.Math.nextAfter() method import java.lang.Math; class GfG { // driver code public static void main(String args[]) { double a = 0.0 / 0; double b = 12.2; // Input a is NaN, Output NaN System.out.println(Math.nextAfter(a, b)); double c = 0.0; double d = 0.0; // Both Input are signed zeros, Output zero System.out.println(Math.nextAfter(c, d)); float e = Float.MIN_VALUE; float f = 12.2f; System.out.println(Math.nextAfter(e, f)); float g = 1.0f / 0f; float h = 1.0f; System.out.println(Math.nextAfter(g, h)); double i = Double.MAX_VALUE; double j = 12344.2; System.out.println(Math.nextAfter(i, j)); } } OutputNaN 0.0 2.8E-45 3.4028235E38 1.7976931348623155E308 Example 2: Java // Java program to demonstrate working // of java.lang.Math.nextAfter() method import java.lang.Math; class GfG { // driver code public static void main(String args[]) { double i = 956.4343; double j = 1234.21; System.out.println(Math.nextAfter(i, j)); } } Output956.4343000000001 Comment More infoAdvertise with us Next Article Java Math nextDown() method with Examples N Niraj_Pandey Follow Improve Article Tags : Java java-math Practice Tags : Java Similar Reads Java Math nextUp() method with Examples The Math.nextUp() method in Java comes under java.lang.Math package. This method returns the floating-point value that is next greater than the given input and moving in the direction of positive infinity. This method is very useful when we need to find the smallest representable number that is grea 3 min read Java Math nextDown() method with Examples The nextDown() is a built-in Math function in Java that returns the next smaller floating-point number adjacent to the parameter provided in the direction of negative infinity. The nextDown() implementation may run faster than its equivalent nextAfter() call. The nextDown() method is overloaded whic 2 min read Scanner match() method in Java with Example The match() method of java.util.Scanner class returns the match result of the last scanning operation performed by this scanner. Syntax: public MatchResult match() Return Value: This function returns a match result for the last match operation. Exceptions: The function throws IllegalStateException i 2 min read Random next() method in Java with Examples The next() method of Random class returns the next pseudorandom value from the random number generator's sequence. Syntax: protected int next(int bits) Parameters: The function accepts a single parameter bits which are the random bits. Return Value: This method returns the next pseudorandom number. 1 min read Scanner nextLine() method in Java with Examples The nextLine() method of java.util.Scanner class advances this scanner past the current line and returns the input that was skipped. This function prints the rest of the current line, leaving out the line separator at the end. The next is set to after the line separator. Since this method continues 2 min read Scanner nextByte() method in Java with Examples The nextByte(radix) method of java.util.Scanner class scans the next token of the input as a Byte. If the translation is successful, the scanner advances past the input that matched. If the parameter radix is not passed, then it behaves similarly as nextByte(radix) where the radix is assumed to be t 5 min read Like