Java.lang.Double.compare() Method


Description

The java.lang.Double.compare() method 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 call −

new Double(d1).compareTo(new Double(d2))

Declaration

Following is the declaration for java.lang.Double.compare() method

public static int compare(double d1, double d2)

Parameters

  • d1 − This is the first double to compare

  • d2 − This is the second double to compare.

Return Value

This method returns the value 0 if d1 is numerically equal to d2; a value less than 0 if d1 is numerically less than d2; and a value greater than 0 if d1 is numerically greater than d2.

Exception

NA

Example

The following example shows the usage of java.lang.Double.compare() method.

package com.tutorialspoint;

import java.lang.*;

public class DoubleDemo {

   public static void main(String[] args) {

      // compares the two specified double values
      double d1 = 15.45;
      double d2 = 11.50;
      int retval = Double.compare(d1, d2);
    
      if(retval > 0) {
         System.out.println("d1 is greater than d2");
      } else if(retval < 0) {
        System.out.println("d1 is less than d2");
      } else {
         System.out.println("d1 is equal to d2");
      }
   }
} 

Let us compile and run the above program, this will produce the following result −

d1 is greater than d2
java_lang_double.htm
Advertisements