Infinite and Not A Number

isInfinite( ) returns true if the value being tested is infinitely large or small in magnitude. isNaN( ) returns true if the value being tested is not a number.

Using the following methods you can find out if a float value is infinite value or it is not a number(NaN).

boolean isInfinite()
Returns true if this Float value is infinitely large in magnitude, false otherwise.
static boolean isInfinite(float v)
Returns true if the specified number is infinitely large in magnitude, false otherwise.
boolean isNaN()
Returns true if this Float value is a Not-a-Number (NaN), false otherwise.
static boolean isNaN(float v)
Returns true if the specified number is a Not-a-Number (NaN) value, false otherwise.

public class Main {
  public static void main(String[] args) {
    Float floatObject2 = Float.valueOf((float)0.0/(float)(0.0));
    System.out.println(floatObject2);
    System.out.println("isInfinite:"+floatObject2.isInfinite());
    System.out.println("isNaN:"+floatObject2.isNaN());
    
  }
}

The output:


NaN
isInfinite:false
isNaN:true

public class Main {
  public static void main(String[] args) {
    Float floatObject2 = Float.valueOf(1/(float)(0.0));
    System.out.println(floatObject2);
    System.out.println("isInfinite:"+floatObject2.isInfinite());
    System.out.println("isNaN:"+floatObject2.isNaN());
    
  }
}

The output:


Infinity
isInfinite:true
isNaN:false
Home 
  Java Book 
    Essential Classes  

Float:
  1. Float class
  2. MAX/MIN_VALUE Find out the Maximum value and Minimum value a float type can have
  3. Create a Float object
  4. Convert Float to byte, double, float, int, long and short
  5. Compare two float objects
  6. Infinite and Not A Number
  7. Convert float value to Hex String value
  8. Convert float value to String value
  9. Convert string value to float value
  10. Bit oriented calculation for float