Home » Java programming language

Java Double class compare() method with example

Double class compare() method: Here, we are going to learn about the compare() method of Double class with its syntax and example.
Submitted by Preeti Jain, on September 21, 2019

Double class compare() method

  • compare() method is available in java.lang package.
  • compare() method is used to check equality or inequality of the given two double values or in other words, we can say this method is used to compare two double values.
  • compare() method is a static method, it is accessible with the class name too and if we try to access the method with the class object then also, we will not get an error.
  • compare() method does not throw an exception at the time of comparing double values.


    public static int compare(double value1, double value2);


  • double value1, double value2 – These parameters represent the double values to be compared.

Return value:

The return type of this method is int, it returns an integer value.

  • In the first case, it returns 0 if value1 is mathematically equals to value2.
  • In the second case, it returns the value < 0 if value1 is mathematically less than value2.
  • In the third case, it returns the value > 0 if value1 is mathematically greater than value2.


// Java program to demonstrate the example 
// of compare(double value1,double value2) 
// method of Double class

public class CompareOfDoubleClass {
    public static void main(String[] args) {
        // Variables initialization
        double value1 = 18.20;
        double value2 = 20.20;

        // It compares two double values and 
        // returns the result in another variable (compare) 
        // of integer types
        int compare = Double.compare(value1, value2);

        // Display result
        System.out.println("Double.compare(value1,value2): " + compare);

        if (compare == 0)
            System.out.println("value1 is equal to value2");
        else if (compare < 0)
            System.out.println("value1 is less than value2");
            System.out.println("value1 is greater than value2");


Double.compare(value1,value2): -1

value1 is less than value2

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.