×

Java Math nextUp() Method

The nextUp() method of Math class returns the floating-point number adjacent to the argument in direction of the positive infinity.

Syntax:

  1. public static double nextUp (double d)
  2. public static float nextUp (float f)

Parameters:

The parameters ‘d’ and ‘f’ represent the starting floating-point values.

Return Value:

The nextUp () method returns the floating-point number which is closer to positive infinity.

Special cases of the nextUp () method are as follows:

  • It returns NaN, is either argument passed is NaN.
  • It returns positive infinity, if the argument is positive infinity.
  • It returns Double.MIN_VALUE, is the argument is zero.

Example 1:

public class JavaMathNextUpExample1 {
    public static void main(String[] args) {
        double d = 2d;
        // returns the floating-point number which is closer to positive infinity
        System.out.println("Floating-point number : "+Math.nextUp(d));
    }
}

Output:

Floating-point number : 2.0000000000000004

Example 2:

public class JavaMathNextUpExample2 {
    public static void main(String[] args) {
        Float f = Float.NEGATIVE_INFINITY;
        // returns the floating-point number which is closer to positive infinity
        System.out.println("Floating-point number : "+Math.nextUp(f));
    }
}

Output:

Floating-point number : -3.4028235E38

Example 3:

public class JavaMathNextUpExample3 {
    public static void main(String[] args) {
        Double d = Double.POSITIVE_INFINITY;
        //returns positive infinity, if the argument is positive infinity
        System.out.println("Floating-point number : "+Math.nextUp(d));
    }
}

Output:

Floating-point number : Infinity

Example 4:

public class JavaMathNextUpExample4 {
    public static void main(String[] args) {
        Double d = Double.NaN;
        //returns NaN, is the argument is NaN.
        System.out.println("Floating-point number : "+Math.nextUp(d));
    }
}

Output:

Floating-point number : NaN

Example 5:

public class JavaMathNextUpExample5 {
    public static void main(String[] args) {
        Double d = 0.0d;
        //returns Double.MIN_VALUE, is the argument is zero.
        System.out.println("Floating-point number : "+Math.nextUp(d));
    }
}

Output:

Floating-point number : 4.9E-324

Related Topics

XOR Binary Operator in Java

One of the various Bitwise operators in Java is ava XOR. If two boolean operands are given, the XOR (also known as exclusive OR) returns true. When both of the...

4 minutes read.

Non-primitive data types in Java

The kind of data stored in the variable is determined by its type. The type describes the data category (different sizes and values). These are not already built into the devices....

4 minutes read.

Aggregation in Java

Aggregation A "has-a" and "whole/part" connection is the best way to define the aggregate relationship in Java, which exists between two classes. This connection relationship has a higher level of specialisation....

4 minutes read.

Median Of Stream Of Running Integers in Java

An integer array is given to us. Compute the median of the elements traversed so far in the input array. For the sake of simplicity, assume that there are no...

10 minutes read.

Annotations in Java

Annotations in Java Java Annotations are metadata about the source code. They do not have any direct effect on the execution of the java program. Annotations in Java were introduced in...

4 minutes read.

How to Concatenate Two Strings in Java

How to Concatenate Two Strings in Java Concatenation of two strings means adding the beginning of one string to the end of the other string. Some of the ways to concatenate...

3 minutes read.

Program to Find Square Root of a Number Without sqrt Method in Java

The Java Math class sqrt () function can be used to determine the square root of a number. In this section, we'll write a Java program to find a number's...

3 minutes read.

How to encrypt password in Java

Every software program needs a username and password to identify a legitimate user. A username can be any number of things, including an email address or a string of characters....

6 minutes read.

Best Java Libraries

One of the most widely used programming languages is Java. Java has a large number of libraries, including the standard Java library that includes libraries such as java.lang, java.util, and...

7 minutes read.

Java String trim() method

Java String trim() method returns String after eliminating leading and trailing spaces. Note:- Java String trim() method doesn't trim or omit middle spaces. Syntax: public String trim() Returns: It returns string with omitted leading and...

2 minutes read.

Java Flags Enum

In a programming language, enumerations represent a group of named constants.For instance; the four suits in a deck of playing cards could be the enumerators Club, Diamond, Heart, and Spade,...

3 minutes read.

Java Applications

The growth in technology is increasing rapidly, so some languages are used for developing them. Java is one such famous programming language which is having numerous applications. The Java Programming...

4 minutes read.

How to Convert long to String in Java

How to Convert long to String in java It is used if we have to display a long number in the text field because everything is displayed as a String. A...

3 minutes read.

Program to Find the Common Elements between two Arrays in Java

In this article, we are going to learn how to find the common elements between two arrays using Java. Here, we use different approaches in Java to find the common...

3 minutes read.

Convert Integer to Roman Numerals in Java

The main objective of this article is to convert the integers that are decimal values to the roman numbers. Problem statement: Write a software/program/code to convert any integer to a roman number. You...

10 minutes read.

How to install Java in Windows 10

To make programs that can run on our systems, we need to install the programming language related software in our systems. Different programming language requires a different type of software aka...

6 minutes read.

How to remove special characters from String in Java

Strings in Java are Objects that are supported inside by a burn exhibit. Since exhibits are immutable (cannot develop), Strings are changeless too. A completely new String is made whenever...

5 minutes read.

Java Exception Propagation

Java Exception Propagation When an exception is being thrown from the peak of the stack and not getting caught, it runs down the stack to the previous method, which is sitting...

3 minutes read.

Java Boolean booleanValue() method

The booleanValue() method of Java Boolean class returns a Boolean value for the specified Boolean argument. Syntax public Boolean booleanValue() Parameters NA Return Value This method returns the primitive value of specified Boolean object. Example 1 public class...

2 minutes read.

Cosmic Superclass in Java

The parent class of all Java classes is the Object class. The Java Object class is the parent of all Java classes, whether directly or indirectly. The Object class is...

6 minutes read.