Compare two double values
In this chapter you will learn:
Compare two double values
Java double type comparison can be done through the following methods:
static int compare(double d1, double d2)
compares the two specified double values.int compareTo(Double anotherDouble)
compares two Double objects numerically.boolean equals(Object obj)
compares this object against the specified object.
static int compare(double d1, double d2)
and
int compareTo(Double anotherDouble)
have the same returns:
Value | Meaning |
---|---|
0 | if anotherDouble is numerically equal to this Double |
less than 0 | if this Double is numerically less than anotherDouble; |
value greater than 0 | if this Double is numerically greater than anotherDouble. |
public class Main {
public static void main(String[] args) {
Double double1 = new Double(1.1);
Double double2 = new Double("1.2");
// j a v a2 s . c om
System.out.println(double1.compareTo(double2));
}
}
The output:
This time we use the static method:
public class Main {
public static void main(String[] args) {
Double double1 = new Double(1.1);
Double double2 = new Double("1.2");
//from jav a2s.c o m
System.out.println(Double.compare(double1,double2));
}
}
The output:
Next chapter...
What you will learn in the next chapter:
- How to find max and min values for double type
- How to create double class with constructor
- How to find out if a double value is an infinite large value, or it is not a number
Home » Java Tutorial » Primitive Data Types