Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Java Tutorial

Java HOME Java Intro Java Get Started Java Syntax Java Output Java Comments Java Variables Java Data Types Java Type Casting Java Operators Java Strings Java Math Java Booleans Java If...Else Java Switch Java While Loop Java For Loop Java Break/Continue Java Arrays

Java Methods

Java Methods Java Method Parameters Java Method Overloading Java Scope Java Recursion

Java Classes

Java OOP Java Classes/Objects Java Class Attributes Java Class Methods Java Constructors Java Modifiers Java Encapsulation Java Packages / API Java Inheritance Java Polymorphism Java Inner Classes Java Abstraction Java Interface Java Enums Java User Input Java Date Java ArrayList Java LinkedList Java HashMap Java HashSet Java Iterator Java Wrapper Classes Java Exceptions Java RegEx Java Threads Java Lambda

Java File Handling

Java Files Java Create/Write Files Java Read Files Java Delete Files

Java How To

Add Two Numbers Count Words Reverse a String Sum of Array Elements Area of Rectangle Even or Odd Number

Java Reference

Java Reference Java Keywords Java String Methods Java Math Methods

Java Examples

Java Examples Java Compiler Java Exercises Java Quiz Java Server Java Certificate


Java Math nextAfter() Method

❮ Math Methods


Example

Find the next floating point number for different numbers in different directions:

System.out.println(Math.nextAfter(1, 2));
System.out.println(Math.nextAfter(1, 0));
System.out.println(Math.nextAfter(0.5f, 1.0f));
System.out.println(Math.nextAfter(0.5f, 0.0f));

Try it Yourself »


Definition and Usage

The nextAfter() method returns the floating point number adjacent to a number x in the direction of a number y.

If y is greater than x then it looks for the lowest possible floating point number that is greater than x. If y is less than x then it looks for the highest possible floating point number that is less than x. If y is equal to x then this method returns x.

The return value for double type arguments will be closer to x than the return value for float type arguments.


Syntax

One of the following:

public static double nextAfter(double x, double y)
public static float nextAfter(float x, double y)

Parameter Values

Parameter Description
x Required. The number to start from.
y Required. The direction to step towards.

Technical Details

Returns: A double or float value representing the next floating point number from a starting point in a specified direction.
Java version: 1.6+

❮ Math Methods