Compare two double values

In this chapter you will learn:

  1. How to compare two double values

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:

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");
// 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:

  1. How to find max and min values for double type
  2. How to create double class with constructor
  3. 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

Introduction

    Java Primitive Data Types

Boolean

    Java boolean type
    Java boolean type conversion
    Convert string value to boolean
    Convert boolean to string

Char

    Java char type
    Compare two char values
    Change char case
    Java char conversion
    Java char value and its attributes

Byte

    Java byte type
    Convert Byte to String
    Convert String to byte
    Byte object constructor
    Byte's max and min value
    Compare two byte values
    Convert Byte to byte, double, float, int, long and short

Short

    Java short type
    Short min/max value and size
    Create Short object
    Compare short values
    Convert short to String
    Convert Short to primitive types
    Convert string to short
    Reverse bytes

Integer

    Java int type
    int max/min value
    Create Java integer
    Convert int to binary, hexadecimal and octal format
    Compare integer values
    Integer sign
    Convert string to int
    Convert int to primitive types
    Convert int to String
    int bit operations

Long

    Java long type
    Compare two long values
    Convert long to binary, hex and octal
    Convert long value to primitive types
    Convert String to long value
    Convert long to String

Float

    Java float type
    Java float type conversion
    Predefined value for float type
    Compare two float value

Double

    Java double type
    Deal with NaN double value
    Compare two double values
    Java double type creation and comparison
    Java double type conversion

Data Type Conversion

    Java Automatic Type Conversion and Casting
    Data type casting
    Java type promotion
    Autoboxing and auto-unboxing