float vs double data types in Java
By: Baski
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.
float
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
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);
}
}
Archived Comments
1. aqatecaxe
View Tutorial By: aqatecaxe at 2017-03-16 16:44:38
2. udoafubhcoro
View Tutorial By: udoafubhcoro at 2017-03-16 16:22:18
3. itogixopiqa
View Tutorial By: itogixopiqa at 2017-03-16 02:42:08
4. apanunocewief
View Tutorial By: apanunocewief at 2017-03-16 02:16:23
5. eqeqeqe
View Tutorial By: eqeqeqe at 2017-03-14 23:16:44
6. agabocehoyovu
View Tutorial By: agabocehoyovu at 2017-03-14 22:50:23
7. emakelabu
View Tutorial By: emakelabu at 2017-03-14 22:45:47
8. meriliqipo
View Tutorial By: meriliqipo at 2017-03-14 22:37:20
9. enamireborite
View Tutorial By: enamireborite at 2017-03-14 22:12:42
10. eqeqeqe
View Tutorial By: eqeqeqe at 2017-03-14 22:01:38
11. buqaqimwo
View Tutorial By: buqaqimwo at 2017-03-14 21:52:06
12. uqolezipa
View Tutorial By: uqolezipa at 2017-03-14 21:43:53
13. You can clear many of your doubts regarding data types in Core Java through Merit Campus, visit: htt
View Tutorial By: Merit Campus at 2016-02-15 07:36:10
14. Actually, the given accuracy just barely exceeds what is possible in a float. A float is stored as:
View Tutorial By: William Fraser at 2015-06-07 16:37:52
15. Hi Robert,
The precision statement applies to numbers that have a large number of dig
View Tutorial By: George at 2015-01-31 06:25:18
16. float should not be use for important precision value, especially on a financial or life threatening
View Tutorial By: Lim Siong Boon at 2014-12-29 16:05:08
17. from docs api api/java/awt/Frame/setOpacity:
public void setOpacity(float opacity)
View Tutorial By: robert at 2014-12-12 07:48:59
18. The Compleate Reference
JAVA Seventh Edition
Author :
Herbert Schildt
View Tutorial By: joshap at 2014-10-25 12:05:41
19. About the dangers of using floats and doubles for currency: a wonderful article that discusses some
View Tutorial By: Hundred Visions Guy at 2014-09-25 04:50:07
20. The Decimal, Double, and Float variable types are different in the way that they store the values. P
View Tutorial By: brianmanee at 2014-09-23 07:04:33
21. "For example, float can be useful when representing dollars and cents."
No.
View Tutorial By: rangzen at 2014-03-28 09:31:47
22. this is a very good artical about floating & double data types for us.
View Tutorial By: Gayathri at 2012-10-31 12:19:28
23. this is a very good artical about floating & double data types for us.
View Tutorial By: Gayathri at 2012-10-31 12:17:05
24. "For example, float can be useful when representing dollars and cents."
Thi
View Tutorial By: David at 2009-10-08 13:47:08
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java