The Artima Developer Community
Sponsored Link

Java Buzz Forum
Difference between float and double java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Difference between float and double java Posted: Aug 21, 2016 9:23 AM
Reply to this message Reply

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.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • 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


float vs double java


 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

  1. package com.instanceofjava.floatvsdouble;

  2. import java.math.BigDecimal;


  3. public class FloatVsDouble {

  4. /**
  5. * @website: www.instanceofjava.com
  6. * @category: float vs double in java with example program
  7. */
  8. public static void main(String[] args) {
  9.     float  a=10.8632667283322234f;
  10.     double b=10.8632667283322234f;
  11.     
  12.      System.out.println("float value="+a);
  13.      System.out.println("double value="+b);
  14.         
  15.      b=10.8632667283322234d;
  16.         
  17.     System.out.println("float value="+a);
  18.     System.out.println("double value="+b);
  19.        

  20. }

  21. }

Output:

  1. float value=10.863267
  2. double value=10.863266944885254
  3. float value=10.863267
  4. double value=10.863266728332224

Difference between float and double java

Read: Difference between float and double java

Topic: The APM market is lively, growing 12% last year Previous Topic   Next Topic Topic: 069: The two types of sales dudes you meet in heaven, the IaaS MQ, and layoffs – Software...

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use