Compare two double values

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:

ValueMeaning
0if anotherDouble is numerically equal to this Double
less than 0if this Double is numerically less than anotherDouble;
value greater than 0if 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");

    System.out.println(double1.compareTo(double2));

  }
}

The output:


-1

public class Main {
  public static void main(String[] args) {
    Double double1 = new Double(1.1);
    Double double2 = new Double("1.2");

    System.out.println(Double.compare(double1,double2));

  }
}

The output:


-1
Home 
  Java Book 
    Essential Classes  

Double:
  1. Double class
  2. Constants in Double class
  3. Double class Constructor
  4. Return double value as byte, double, float, int, long, short
  5. Compare two double values
  6. Is a double value an infinite large value, or it is not a number.
  7. Convert string value to double
  8. Convert double value from string
  9. Get the hexadecimal string representation
  10. Bit oriented calculation for double type