float vs double data types in Java
By: Baski Emailed: 1768 times Printed: 2515 times
Latest comments

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 floatingpoint types is examined next.
float
The type float specifies a singleprecision 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 highspeed 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 largevalued 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);
}
}
Java Home  All Java Tutorials  Latest Java Tutorials
Sponsored Links 
If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Comments(7)
1.  View Comment"For example, float can be useful when representing dollars and cents." View Tutorial By: David at 20091008 13:47:08 
2.  View Commentthis is a very good artical about floating & double data types for us. View Tutorial By: Gayathri at 20121031 12:19:28 
3.  View Comment"For example, float can be useful when representing dollars and cents." View Tutorial By: rangzen at 20140328 09:31:47 
4.  View CommentThe Compleate Reference View Tutorial By: joshap at 20141025 12:05:41 
5.  View Commentfrom docs api api/java/awt/Frame/setOpacity: View Tutorial By: robert at 20141212 07:48:59 
6.  View CommentHi Robert, View Tutorial By: George at 20150131 06:25:18 
7.  View CommentActually, the given accuracy just barely exceeds what is possible in a float. A float is stored as: 1 sign bit, 23 mantissa bits, 1 sign bit for the exponent, and 7 bits for the exponent value. View Tutorial By: William Fraser at 20150607 16:37:52 
Most Viewed Articles (in Java )

Most Emailed Articles (in Java)
