Double compare() Method in Java with Examples Last Updated : 08 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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) Comment More infoAdvertise with us Next Article Double compare() Method in Java with Examples G gopaldave Follow Improve Article Tags : Java Java-lang package Java-Functions Java-Double Practice Tags : Java Similar Reads Java Guava | Doubles.compare() method with Examples Doubles.compare() method of Guava's Doubles Class is used to compare the two specified double values. These values are passed as the parameter and the result of comparison is found as the difference of 1st value and the 2nd value. Hence it can be positive, zero or negative. Syntax: public static int 2 min read Double byteValue() Method in Java with Examples The Double.byteValue() is a built-in method in Java Double class. This method converts the value of a Double object to a byte type. Basically, it is used for narrowing the primitive conversion of the Double type to a byte value.In this article, we are going to learn about the Double.byteValue() meth 3 min read DoubleAdder add() method in Java with Examples The java.DoubleAdder.add() is an inbuilt method in java that adds the given value to the previous or initial value. When the object of the class is created its initial value is zero. Syntax: public void add(double x) Parameters: This method accepts a single parameter x that specifies the value to be 1 min read Double doubleValue() method in Java with examples The doubleValue() method of Double class is a built in method to return the value specified by the calling object as double after type casting. Syntax: DoubleObject.doubleValue() Return Value: It return the value of DoubleObject as double. Below programs illustrate doubleValue() method in Java: Prog 2 min read Byte doubleValue() method in Java with examples The doubleValue() method of Byte class is a built in method in Java which is used to return the value of this Byte object as double. Syntax ByteObject.doubleValue() Return type: It returns the value of ByteObject as double. Below is the implementation of doubleValue() method in Java: Example 1: Java 2 min read Like