Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Java Floating Data Types

The floating data type represent the floating numbers that means numbers with decimal point .For example: 1.45, 3.141, 0.005, -125.12 etc. There are 2 types of floating datatypes such as float and double which represent single and double precision numbers respectively.
The following table shows the memory size and range of float and double.

sl.no Datatype Memory size Range
1 Float 4bytes -3.4e38 to -1.4e-45 for ?ve values and 1.4e-45 to 3.4e38 for +ve values
2 Double 8bytes -1.8e308 to -4.9e-324 for negative values and 4.9 e -324 to 1.8e308 for positive values


What is the difference between float and double?

Float can represent up to 7digit accurately after decimal point, where as double can represent up to 15 digit accurately after decimal point.
Let us consider this statement
float x = 1.25f;


Here the variable x contains the value 1.25.

If f is not written at the end, then JVM will allocate 8 bytes because JVM will assume the values double. The reason is in java in float and double datatypes, the default it is taken as double. So for f, JVM considers it as float and allocate 4bytes of memory.

Let's see a simple program which uses double variable to find the area of a circle.

Area of the circle
class Area
{
	public static void main (String args[])
	{
		double pi, r, a;
		r=5.2;
		pi=3.141;
		a=pi*r*r;
		system.out.println(?area is?+a);
	}
}
Output

area is 15

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext