Java float type

In this chapter you will learn:

  1. What is Java float type
  2. How to create float literals
  3. How to Create a Float object
  4. How to tell if a float is infinite or finite
  5. How to convert float value to int bits

float type

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    
    } /*from  j  av  a2 s . com*/
}

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());
    // ja  va2s.  c  om
    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();
    
  }
}

Float class wraps a value of type float in an object.

Create a Float object

We can create Float object in three ways:

  • Float(double value) creates a Float object from double-type value and convert it to type float.
  • Float(float value) creates a Float object for the primitive float argument.
  • Float(String s) creates a Float object that represents the floating-point value of type float represented by the string.

The following code creates three Float objects. Pay attention to the different type of values it is using.

public class Main {
  public static void main(String[] args) {
    Float float1 = new Float(0.0d);
    Float float2 = new Float(0.1F);
    Float float3 = new Float("0.2");
    //from  j  a v  a  2s .com
    System.out.println(float1);
    System.out.println(float2);
    System.out.println(float3);
    
  }
}

The output:

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.

Let's look at how to use the methods listed above.

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

The output:

More examples on isInfinite().

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

The output:

Bit oriented calculation for float

  • static int floatToIntBits(float value) returns a representation of the specified floating-point value according to the IEEE 754 floating-point "single format" bit layout.
  • static int floatToRawIntBits(float value) returns a representation of the specified floating-point value according to the IEEE 754 floating-point "single format" bit layout, preserving Not-a-Number (NaN) values.
  • static float intBitsToFloat(int bits) returns the float value corresponding to a given bit representation.
public class Main {
  public static void main(String[] args) {
    int i = Float.floatToIntBits(1.1F);
    System.out.println(i);/*from   j  a  v a 2s . com*/
    
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to convert Float to byte, double, float, int, long and short
  2. How to convert float value to Hex String value
  3. How to convert float value to String value
  4. How to convert string value to float value
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