Java Math nextAfter() method with Example Last Updated : 30 May, 2023 Comments Improve Suggest changes 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 Create Quiz Comment N Niraj_Pandey Follow 0 Improve N Niraj_Pandey Follow 0 Improve Article Tags : Java java-math Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like