float vs double data types in Java

By: Baski Printer Friendly Format    

Floating-point numbers, also known as real numbers, are used when evaluating expressions that require fractional precision. For example, calculations such as square root, or transcendentals such as sine and cosine, result in a value whose precision requires a floating-point type. Java implements the standard (IEEE–754) set of floatingpoint types and operators. There are two kinds of floating-point types, float and double, which represent single- and double-precision numbers, respectively. Their width and ranges are shown here:


Name Width in Bits Range
double 64 1 .7e–308 to 1.7e+308
float 32 3 .4e–038 to 3.4e+038

Each of these floating-point types is examined next.


The type float specifies a single-precision value that uses 32 bits of storage. Single precision is faster on some processors and takes half as much space as double precision, but will become imprecise when the values are either very large or very small. Variables of type float are useful when you need a fractional component, but don't require a large degree of precision. For example, float can be useful when representing dollars and cents.

Here are some example float variable declarations:

float hightemp, lowtemp;


Double precision, as denoted by the double keyword, uses 64 bits to store a value. Double precision is actually faster than single precision on some modern processors that have been optimized for high-speed mathematical calculations. All transcendental math functions, such as sin( ), cos( ), and sqrt( ), return double values. When you need to maintain accuracy over many iterative calculations, or are manipulating large-valued numbers, double is the best choice.

Here is a short program that uses double variables to compute the area of a circle:

// Compute the area of a circle.
class Area {
public static void main(String args[]) {
double pi, r, a;
r = 10.8; // radius of circle
pi = 3.1416; // pi, approximately
a = pi * r * r; // compute area

System.out.println("Area of circle is " + a);



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. "For example, float can be useful when repres
View Tutorial          By: David at 2009-10-08 13:47:08

2. this is a very good artical about floating &
View Tutorial          By: Gayathri at 2012-10-31 12:17:05

3. this is a very good artical about floating &
View Tutorial          By: Gayathri at 2012-10-31 12:19:28

4. "For example, float can be useful when repres
View Tutorial          By: rangzen at 2014-03-28 09:31:47

5. The Decimal, Double, and Float variable types are
View Tutorial          By: brianmanee at 2014-09-23 07:04:33

6. About the dangers of using floats and doubles for
View Tutorial          By: Hundred Visions Guy at 2014-09-25 04:50:07

7. The Compleate Reference
JAVA Seventh Editi

View Tutorial          By: joshap at 2014-10-25 12:05:41

8. from docs api api/java/awt/Frame/setOpacity:

View Tutorial          By: robert at 2014-12-12 07:48:59

9. float should not be use for important precision va
View Tutorial          By: Lim Siong Boon at 2014-12-29 16:05:08

10. Hi Robert,

The precision statement

View Tutorial          By: George at 2015-01-31 06:25:18

11. Actually, the given accuracy just barely exceeds w
View Tutorial          By: William Fraser at 2015-06-07 16:37:52

12. You can clear many of your doubts regarding data t
View Tutorial          By: Merit Campus at 2016-02-15 07:36:10

13. uqolezipa
View Tutorial          By: uqolezipa at 2017-03-14 21:43:53

14. buqaqimwo
View Tutorial          By: buqaqimwo at 2017-03-14 21:52:06

15. eqeqeqe
View Tutorial          By: eqeqeqe at 2017-03-14 22:01:38

16. enamireborite
View Tutorial          By: enamireborite at 2017-03-14 22:12:42

17. meriliqipo
View Tutorial          By: meriliqipo at 2017-03-14 22:37:20

18. emakelabu
View Tutorial          By: emakelabu at 2017-03-14 22:45:47

19. agabocehoyovu
View Tutorial          By: agabocehoyovu at 2017-03-14 22:50:23

20. eqeqeqe
View Tutorial          By: eqeqeqe at 2017-03-14 23:16:44

21. apanunocewief
View Tutorial          By: apanunocewief at 2017-03-16 02:16:23

22. itogixopiqa
View Tutorial          By: itogixopiqa at 2017-03-16 02:42:08

23. udoafubhcoro
View Tutorial          By: udoafubhcoro at 2017-03-16 16:22:18

24. aqatecaxe
View Tutorial          By: aqatecaxe at 2017-03-16 16:44:38