Java Program to check if a Float is Infinite or Not a Number(NAN)


To check if a Float is isInfinite, use the isInfinite() method and to check for NAN, use the isNaN() method.

Example

 Live Demo

public class Demo {
   public static void main(String[] args) {
      float value1 = (float) 1 / 0;
      boolean res1 = Float.isInfinite(value1);
      System.out.println("Checking for isInfinite? = "+res1);
      float value2 = (float) Math.sqrt(9);
      boolean res2 = Float.isNaN(value2);
      System.out.println("Checking for isNan? = "+res2);
   }
}

Output

Checking for isInfinite? = true
Checking for isNan? = false

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements