The floating-point number are those number which needs fractional precision that is the number that can be in the fraction. There many more mathematical calculations in which floating type is involved. For e.g. finding the square root of a number up to certain decimal values finding the cube root of the number, finding roots of quadratic equation, and calculation involving trigonometric functions like sin cos tan.
There are two types of floating-point data type
|Float||32||1.4e–045 to 3.4e+038|
|Double||64||4.9e–324 to 1.8e+308|
Float: It is a single-precision value that has 32 bits in storage. and this single precision is faster and does take less size compared to double precision.
For java variables, we can use float while declaring or initializing for expected value to be fractional. The default value in java is 0.0f and its size is 4 bytes. Float in java can have negative values.
The correct ways and incorrect ways of defining java floating-point.
- float a1=10.57f which is equal to 10.57
- float a2 =10f it is equal to 10.0
- float a3=9.58 it will give an error.
Here we concluded that if we are initializing any value with float it ends with f then it is correct otherwise it will throw an error.
Printing float values:
Start range: 1.4E-45 End range: 3.4028235E38
Multiplication of float values:
The result of n1 x n2 is: 80.912704
Providing value in Scientific notation
Simple Float: 283.75 Scientific Notation: 283.75
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.