Java float / double data type



Float Data Type :

  1. Floating Data Type is used to store float value.
  2. Floating Data Type is Primitive Data Type in Java Programming Language.
  3. Floating Data Type have respective Wrapper Class – “Float or Double“.

float Data Type Can store 2 types of Values these are listed below –

  1. Float
  2. Double

[468×60]

floatVariables of this type can have values from -3.4E38 (-3.4 * 1038) to +3.4E38 (+3.4 * 1038) and occupy 4 bytes in  memory. Values are represented with approximately 7 decimal digits accuracy
doubleVariables of this type can have values from -1.7E308 (-1.7 * 10308) to +1.7E308 (+1.7 * 10308) and occupy 8 bytes in memory. Values are represented with approximately 17 decimal digits accuracy. The smallest non-zero value that you can have is roughly (4.9 * 10–324).

Live Example : Declaring Integer Variable in Java Programming

Class FloatDemo
{
public static void main(String args[])
  {
  float fval = 10.0f;
  System.out.println("Total Number : " + fval);
  }
}

Float Type : Some Notes

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

Way 1 : How to Declare Double Variable

Class FloatDemo
{
public static void main(String args[])
  {
  double d1 = 10;
  System.out.println("Total Number : " + fval);
  }
}
  • We have assigned integer value to the Double . (i.e we are assigning lower value of inside bigger variable , no need to typecast )
  • double keyword is used to declare double variable.

Way 2 : How to Declare float Variable

float fval = 10.4F;

Way 3 : Using Exponent In Double Value

float electronMass = 9E-28F;

another example

double lightSpeed = 3.8E8;

Way 4 : Declaring Fix Value

final int meter_in_cm = 100;