- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Double compare example
To compare Double in Java, use the following methods −
Method 1 − compareTo() method in Java
The java.lang.Double.compareTo() method compares two Double objects.
Example
import java.lang.*; public class Demo { public static void main(String args[]) { Double d1 = new Double("395.24"); Double d2 = new Double("323.30"); int res = d1.compareTo(d2); if(res > 0) { System.out.println("d1 is greater than d2"); } else if(res < 0) { System.out.println("d1 is less than d2"); } else { System.out.println("d1 is equal to d2"); } } }
Output
d1 is greater than d2
Method 2 − compare() method in Java
The java.lang.Double.compare() method compares two double values objects numerically.
Example
import java.lang.*; public class Demo { public static void main(String args[]) { Double d1 = new Double("195.24"); Double d2 = new Double("323.30"); int res = Double.compare(d1, d2); if(res > 0) { System.out.println("d1 is greater than d2"); } else if(res < 0) { System.out.println("d1 is less than d2"); } else { System.out.println("d1 is equal to d2"); } } }
Output
d1 is less than d2
- Related Articles
- Compare Two Java double arrays
- Compare two double arrays in a single line in Java
- How to compare float and double in C++?
- In how many ways can I compare Strings in Java explain with example?
- Give an example of a double displacement reaction.
- Convert double primitive type to a Double object in Java
- How to Convert Double to String to Double in Java?
- Double isInfinite() method in Java
- Double isNaN() method in Java
- Format double type in Java
- Double brace initialization in Java
- Compare Dates in Java
- Java Integer compare() method
- Convert double to string in Java
- Convert Double to Integer in Java

Advertisements