Float / Double Data Type : Java Programming Language
Float Data Type :
- Floating Data Type is used to store float value.
- Floating Data Type is Primitive Data Type in Java Programming Language.
- Floating Data Type have respective Wrapper Class - “Float or Double“.
float Data Type Can store 2 types of Values these are listed below -
- Float
- Double
float | Variables 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 |
double | Variables 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
- 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.
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;