Floating-Point Data Types - Java Language Basics

Java examples for Language Basics:float

Introduction

Java has two floating-point Numeric data types:

  • float
  • double

The float Data Type

Java float data type uses 32 bits to store a floating-point number in the IEEE 754 standard format.

All real numbers that end with f or F are called float literals.

A float literal can be expressed in the following two formats:

  • Decimal number format
  • Scientific notation

Examples of float literals in decimal number format are as follows:

float f1 = 8F; 
float f2 = 8.F; 
float f3 = 8.0F; 
float f4 = 3.51F; 
float f5 = 0.0F; 
float f6 = 16.78f; 
  

Real number 3.25 is also written using exponential forms such as 32.5 x 10^-1 or 0.325 x 10^1.

In scientific notation, the number 32.5 x 10^-1 is written as 32.5E-1.

As a float literal, it can be written as 32.5E-1F or 32.5E-1f.

All of the following float literals denote the same real number 32.5:

3.25F 
32.5E-1F 
0.325E+1F 
0.325E1F 
0.0325E2F 
0.0325e2F 
3.25E0F 
  

The float data type defines two zeros: +0.0F (or 0.0F) and -0.0F.

For the comparison purposes, both +0.0F and -0.0F are considered equal.

The float data type defines two infinities: positive infinity and negative infinity.

For example, the result of the dividing 2.5F by 0.0F is a float positive infinity whereas the result of dividing 2.5F by -0.0F is a float negative infinity.

Results of some of the operations on float are not defined. For example, dividing 0.0F by 0.0F is indeterminate.

Indeterminate results are represented by a special value of the float data type called NaN (Not-a-Number).

Java Float class defines three constants that represent positive infinity, negative infinity, and NaN of the float data type.

The following table lists these three float constants and their meanings.

float ConstantsMeaning
Float.POSITIVE_INFINITYPositive infinity of type float
Float.NEGATIVE_INFINITYNegative infinity of type float
Float.NaN Not a Number of type float
Float.MAX_VALUEThe largest positive value that can be represented in a float variable.
Float.MIN_VALUEThe smallest positive value greater than zero that can be represented in a float variable.

The value of all integral types (int, long, byte, short, and char) can be assigned to a variable of the float data type without using an explicit cast.

int num1 = 15; 
float salary = num1;            // Ok. int variable to float 
salary = 12455;                 // Ok. int literal to float 
float bigNum = Float.MAX_VALUE; // Assigns maximum float value 
bigNum = 1226L;                 // Ok, a long literal to float 
float justAChar = 'A';          // Ok. Assigns 65.0F to justAChar 
float fInf = Float.POSITIVE_INFINITY;  // Ok. Assigns positive infinity to the fInf variable 
float fNan = Float.NaN;   // Ok. Assigns Not-a-Number to fNan variable 
float fTooBig = 3.5E38F;  // A compile-time error. too large
float fTooSmall = 1.4E-46F;  // A compile-time error. too small
  

A float value must be cast before it is assigned to a variable of any integral data type int, long, byte, short, or char.

int num1 = 10; 
float salary = 10.0F; 
num1 =  salary;      // An error. Cannot assign float to int 
num1 = (int)salary;  // Ok

Related Tutorials