Compare two float value

In this chapter you will learn:

  1. How to compare two float objects
  2. How to compare float value to a NaN(Not a Number)

Compare two float objects

We have three ways to compare float values:

  • static int compare(float f1, float f2) compares the two specified float values.
  • int compareTo(Float anotherFloat) compares two Float objects numerically.
  • boolean equals(Object obj) compares this object against the specified object.

static int compare(float f1, float f2) compares the two specified float values. It returns:

ValueMeaning
0if f1 is numerically equal to f2;
less than 0if f1 is numerically less than f2;
greater than 0if f1 is numerically greater than f2.

The code below uses the compareTo(Float anotherFloat) method to compare two Float objects.

public class Main {
  public static void main(String[] args) {
    Float floatObject1 = new Float("10.0001");
    Float floatObject2 = new Float("10.0002");
    //from j a  va  2  s  . c  om
    System.out.println(Float.compare(floatObject1,floatObject2));
    
  }
}

The output:

Compare float value to a NaN(Not a Number)

The following code create a NaN(Not a Number) float value by dividing 0, and then compare it with another float value:

public class Main {
  public static void main(String[] args) {
    Float floatObject1 = new Float("10.0001");
    //from ja  va 2 s  . co m
    Float floatObject2 = Float.valueOf((float)0.0/(float)(0.0));
    System.out.println(floatObject2);
    
    System.out.println(Float.compare(floatObject1,floatObject2));
    
  }
}

The output:

int compareTo(Float anotherFloat) compares two Float objects numerically. Float.NaN is considered to be equal to itself and greater than all other float values including Float.POSITIVE_INFINITY. 0.0f is considered by this method to be greater than -0.0f.

int compareTo(Float anotherFloat) returns:

  • 0 if anotherFloat is numerically equal to this Float;
  • less than 0 if this Float is numerically less than anotherFloat;
  • greater than 0 if this Float is numerically greater than anotherFloat.

The following code illustrates how to create NaN in float and compares to another float type value.

public class Main {
  public static void main(String[] args) {
    Float floatObject1 = new Float("10.0001");
    //  j  a  va  2  s.  c  o m
    Float floatObject2 = Float.valueOf((float)0.0/(float)(0.0));
    System.out.println(floatObject2);
    
    System.out.println(floatObject1.compareTo(floatObject2));
    
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. What is Java double type
  2. How to create double type Literals
  3. How to get the value of double infinity
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