Friday, September 5, 2025
HomeLanguagesJavaDouble compare() Method in Java with Examples

Double compare() Method in Java with Examples

The compare() method of Double Class is a built-in method in Java that compares the two specified double values. The sign of the integer value returned is the same as that of the integer that would be returned by the function call. 

Syntax: 

public static int compare(double d1, double d2)

Parameters: The function accepts two parameters:  

  • d1: The first double value to be compared.
  • d2: The second double value to be compared.

Return Value: The function returns value as below:  

  • 0: if d1 is numerically equal to d2.
  • Negative value: if d1 is numerically less than d2.
  • Positive value: if d1 is numerically greater than d2.

Below programs illustrates the use of Double.compare() function:

Program 1: When two integers are same  

Java




// Java Program to illustrate
// the Double.compare() method
 
import java.lang.Double;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Get the two double values
        // to be compared
        Double d1 = 1023d;
        Double d2 = 1023d;
 
        // function call to compare two double values
        if (Double.compare(d1, d2) == 0) {
 
            System.out.println("d1=d2");
        }
        else if (Double.compare(d1, d2) < 0) {
 
            System.out.println("d1<d2");
        }
        else {
 
            System.out.println("d1>d2");
        }
    }
}


Output: 

d1=d2

 

Program 2 : When d1<d2

Java




// Java Program to illustrate
// the Double.compare() method
 
import java.lang.Double;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Get the two double values
        // to be compared
        Double d1 = 10d;
        Double d2 = 1023d;
 
        // function call to compare two double values
        if (Double.compare(d1, d2) == 0) {
 
            System.out.println("d1=d2");
        }
        else if (Double.compare(d1, d2) < 0) {
 
            System.out.println("d1<d2");
        }
        else {
 
            System.out.println("d1>d2");
        }
    }
}


Output: 

d1

Program 3 : When d1>d2

Java




// Java Program to illustrate
// the Double.compare() method
 
import java.lang.Double;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Get the two double values
        // to be compared
        Double d1 = 1023d;
        Double d2 = 10d;
 
        // function call to compare two double values
        if (Double.compare(d1, d2) == 0) {
 
            System.out.println("d1=d2");
        }
        else if (Double.compare(d1, d2) < 0) {
 
            System.out.println("d1<d2");
        }
        else {
 
            System.out.println("d1>d2");
        }
    }
}


Output: 

d1>d2

 

Reference: https://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#compare(double, %20double)
 

RELATED ARTICLES

Most Popular

Dominic
32265 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11864 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS