float Data Type in Java Programming Language

Rumman Ansari   Software Engineer   2019-03-30   15899 Share
☰ Table of Contents

Table of Content:


float Data Type in Java

Floating-point numbers, also known as real numbers, are used when evaluating expressions thatrequire fractional precision.There are two kinds of floating-point types, float and double, which represent single- and double-precision numbers, respectively.

  • float Data Type is Primitive Data Type in Java Programming Language.

  • Float data type is a single-precision 32-bit IEEE 754 floating point

  • Float is mainly used to save memory in large arrays of floating point numbers

  • Default value is 0.0f

  • Float data type is never used for precise values such as currency

  • Floating Data Type have respective Wrapper Class – "Float or Double".

  • Example:

     float f1 = 234.5f 

Important Point About float Data Type

In Java any value declared with decimal point is by default of type double. Suppose we have to assign float value then we must use ‘f’ or ‘F’ literal to specify that current value is “Float”. Specify "E" or "e" for values which contain exponent.

How to Declare float Variable

float fval = 10.4F;

Using Exponent In Double Value

float electronMass = 9E-28F;
another
double lightSpeed = 3.8E8;

Data TypeSize(Byte)Range
float4 +2147483647 to -2147483648

float Variable Declaration and Variable Initialization:

Variable Declaration : To declare a variable , you must specify the data type & give the variable a unique name.

 float pi ;  

Variable Initialization : To initialize a variable you must assign it a valid value.

 float = 3.14 ;  

float Variable Declaration and Variable Initialization in two steps:

float pi;
pi = 3.14;
float data type in java

Example :

Save Source File Name as : FloatExample1.java
To compile : javac FloatExample1.java
To Run : java FloatExample1
// A Java program to demonstrate float data type
class FloatExample1 {
   public static void main(String args[])
     {
           float pi;
           pi = 3.14f;
           System.out.println(pi);
     }
  }
  
Output
3.14
press any key to continue ... 
 
float data type in java

float Variable Declaration and Variable Initialization in one line:

float pi = 3.14;
float data type in java

Example :

Save Source File Name as : FloatExample2.java
To compile : javac FloatExample2.java
To Run : java FloatExample2
// A Java program to demonstrate float data type
class FloatExample2 {
   public static void main(String args[])
     {
           float pi = 3.14f;
           System.out.println(pi);
     }
  }
Output
3.14
press any key to continue ...

Example of float

public class FloatDataType {

   public static void main(String []args) {
      // this is declaration and initialization of variable a
	  // datatype is float
	  float a = 234.5f;
	  // this is declaration and initialization of variable b
	  // datatype is float
      float b = 234.565f;
      System.out.println(a); // it will print a variable
      System.out.println(b); // it will print b variable
      
   }
}
Output
234.5
234.565
Press any key to continue . . .