Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
JavaTuples compareTo() method
Next article icon

Java Double.compareTo() Method

Last Updated : 15 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Double.compareTo() method is a built-in method in Java of java.lang package. This method is used to compare two Double objects numerically. This method returns 0 if this object is equal to the argument object, it returns less than 0 if this object is numerically less than the argument object, and a value greater than 0 if this object is numerically greater than the argument object. 

In this article, we are going to learn about the Double.compareTo() method in Java with examples.

Syntax of Double.compareTo() Method

public int compareTo(Double anotherDouble)

Parameter: obj: The object that is to be compared to. 

Return Values: The function can return three values:

  • equal to 0: Object is equal to the argument object.
  • less than 0: Object is less than the argument object.
  • greater than 0: Object is greater than the argument object.

Note: This method only accepts a Double object as an argument. If we pass anything else or no argument at all, it will result in a compile-time error.

Examples of Java Double.compareTo() Method

Example 1: In this example, we are going to compare two different Double objects.

Java
// Java program to demonstrate Double.compareTo() method import java.lang.Double;  public class Geeks {          public static void main(String[] args) {                  Double obj1 = new Double(124);         Double obj2 = new Double(167);          int compareValue = obj1.compareTo(obj2);          if (compareValue == 0)             System.out.println("obj1 and obj2 are equal");         else if (compareValue < 0)             System.out.println("obj1 is less than obj2");         else             System.out.println("obj1 is greater than obj2");     } } 

Output
obj1 is less than obj2 


Example 2: In this example, we are going to pass no argument and see what happens.

Java
// No argument passed import java.lang.Double;  public class Geeks {          public static void main(String[] args) {         Double obj1 = new Double(124);          // This will result in a compile-time error         int result = obj1.compareTo();      } } 

Output:

error: method compareTo in class Double cannot be applied to given types;
int result = obj1.compareTo();
^
required: Double
found: no arguments
reason: actual and formal argument lists differ in length


Example 3: In this example, we are going to pass a non-Double object.

Java
// Passing incompatible type import java.lang.Double;  public class Geeks {          public static void main(String[] args) {         Double obj1 = new Double(124);          // This will also result in a compile-time error         int result = obj1.compareTo("gfg");      } } 

Output:

error: incompatible types: String cannot be converted to Double


Important Points:

  • Always make sure the object passed to compareTo() method is of type Double.
  • Do not use raw objects like Object in compareTo() for wrapper classes, always prefer using the typed version.
  • For comparing primitive double values directly, consider using Double.compare(double d1, double d2).

Next Article
JavaTuples compareTo() method

T

Twinkl Bajaj
Improve
Article Tags :
  • Java
  • Java-lang package
  • Java-Functions
Practice Tags :
  • Java

Similar Reads

    Java Integer compareTo() method
    The compareTo() method of Integer class of java.lang package compares two Integer objects numerically and returns the value 0 if this Integer is equal to the argument Integer; a value less than 0 if this Integer is numerically less than the argument Integer; and a value greater than 0 if this Intege
    2 min read
    Double.compareTo() Method in Java with Examples
    The Double.compareTo() method is a built-in method in Java of java.lang package. This method is used to compare two Double objects numerically. This method returns 0 if this object is equal to the argument object, it returns less than 0 if this object is numerically less than the argument object, an
    3 min read
    JavaTuples compareTo() method
    The compareTo() method in org.javatuples is used to compare the order of a Tuple object with the object passed as the parameter. This method can be used for any tuple class object of javatuples library. It returns the difference in the ASCII value of the 1st different element present in the passed o
    3 min read
    Java Integer compare() method
    The compare() method of Integer class of java.lang package compares two integer values (x, y) given as a parameter and returns the value zero if (x==y), if (x < y) then it returns a value less than zero and if (x > y) then it returns a value greater than zero. Syntax : public static int compar
    2 min read
    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:
    2 min read
    YearMonth compareTo() method in Java
    The compareTo() method of YearMonth class in Java is used to compare two YearMonth objects. It compares this YearMonth object to the YearMonth object passed to it as parameter. The comparison between the two YearMonth instances is first done on the value of Year and then on the Month. Syntax: public
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences