❮Previous Next ❯

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 consider it as float and allocate 4bytes of memory.

Let's see a simple program which uses double variable to fined the area of 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

❮Previous Next ❯

Follow Us




  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved