float

float type represents single-precision numbers. float is 32-bit width and its range is from 1.4e-045 to 3.4e+038 approximately.

float type variables are useful when you need a fractional component. Here are some example float variable declarations:

float high, low;

float literals

Floating-point literals in Java default to double precision. To specify a float literal, you must append an F or f to the constant.


public class Main { 
    public static void main(String args[]) { 
        float d =  3.14159F; 
        System.out.print(d);//3.14159    
    } 
}

Java's floating-point calculations are capable of returning +infinity, -infinity, +0.0, -0.0, and NaN


public class Main {
  public static void main(String[] args) {
    Float f1 = new Float(Float.NaN);
    System.out.println(f1.floatValue());
    
    Float f2 = new Float(Float.NaN);
    System.out.println(f2.floatValue());
    System.out.println(f1.equals(f2));
    System.out.println(Float.NaN == Float.NaN);
    System.out.println();
    
  }
}
Home 
  Java Book 
    Language Basics  

Primitive Types:
  1. The Primitive Types
  2. byte
  3. short
  4. int
  5. long
  6. float
  7. double
  8. char
  9. boolean