Home » Java programming language

# Java Double class compareTo() method with example

**Double class compareTo() method**: Here, we are going to learn about the **compareTo() method of Double class** with its syntax and example.

Submitted by Preeti Jain, on September 24, 2019

## Double class compareTo() method

**compareTo() method**is available in java.lang package.**compareTo() method**is used to check equality or inequality for this Double-object against the given Double-object mathematically or in other words, we can say this method is used to compare two Double objects.**compareTo() method**is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.**compareTo() method**does not throw an exception at the time of comparing Double-object.

**Syntax:**

public int compareTo(Double value2);

**Parameter(s):**

- Double value2 – represents the Double object to compare with.

**Return value:**

The return type of this method is int, it returns an integer value. It returns **0** if value2 is mathematically equal to value1 (this object), it returns **value < 0** if value2 is mathematically greater than value1 (this object), it returns **value > 0** if value2 is mathematically less than value1 (this object).

**Example:**

// Java program to demonstrate the example // of compareTo(Double value2) method of Double class public class CompareToOfDoubleClass { public static void main(String[] args) { // Variables initialization double d1 = 18.20; double d2 = 20.20; // Double instance Double value1 = new Double(d1); Double value2 = new Double(d2); // It compare two Double objects and placed the // result in another variable (compare) of integer type int compare = value1.compareTo(value2); // Display result System.out.println("value1.compareTo(value2): " + compare); System.out.println(); if (compare == 0) System.out.println("value1 is equal to value2"); else if (compare < 0) System.out.println("value1 is less than value2"); else System.out.println("value1 is greater than value2"); } }

**Output**

value1.compareTo(value2): -1 value1 is less than value2

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

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