Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

nextAfter()

Example

Calculate the subsequent floating-point number in varying directions for different input values.

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

Definition and Usage

The nextAfter() method in Java retrieves the floating-point number that is adjacent to a given number in the direction specified by another number .

If is greater than , the method finds the next smallest floating-point number greater than . If is less than , it identifies the next largest floating-point number smaller than . If equals , the method returns .

Notably, for double type arguments, the returned value will be closer to than 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

Please provide the starting number.

y

Please specify the direction to move towards.

Technical Details

Returns:

A double or float value representing the subsequent floating-point number from a given starting point in a specified direction.

Java Version:

1.6+