This post originated from an RSS feed registered with Java Buzz
by instanceof java.
Original Post: Difference between float and double java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Float data type in java is represented in 32 bits, with 1 sign bit, 8 bits of exponent, and 23 bits of the mantissa
Where as Double is is represented in 64 bits, with 1 sign bit, 11 bits of exponent, and 52 bits of mantissa.
Default value of float is 0.0f.
Default value of double is 0.0d.
Floating points numbers also known as real numbers and in java there are two types of floating point one is float and another one is double.
Float specifies single precision and double specifies double precision.
According to the IEEE standards, float is a 32 bit representation of a real number while double is a 64 bit representation
Normally we use double instead of float to avoid common overflow of range of numbers
Check below diagram for width and range of float and double data types
When do you use float and when do you use double:
Use double data type for all your calculations and temp variables.
Use float when you need to maintain an array of numbers - float[] array (if precision is sufficient), and you are dealing with over tens of thousands of float numbers.
Most of the math functions or operators convert/return double, and you don't want to cast the numbers back to float for any intermediate steps.
How many significant digits have floats and doubles in java?
In java float can handle about 7 decimal places.
And double can handle about 16 decimal places
Program #1: write a java example program which explains differences between float and double in java
package com.instanceofjava.floatvsdouble;
import java.math.BigDecimal;
public class FloatVsDouble {
/**
* @website: www.instanceofjava.com
* @category: float vs double in java with example program