

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
When can a double-type be preferred over float-type in Java?
Both double-type and float-type can be used to represent floating-point numbers in Java. A double-type is preferred over float-type if the more precise and accurate result is required. The precision of double-type is up to 15 to 16 decimal points while the precision of float type is only around 6 to 7 decimal digits. The double-type can be used for all the calculations and temp variables while a float-type can be used to maintain an array of numbers. A double-type uses 1 bit for a sign and 11 bits for exponent while float-type only uses 1 bit for a sign and 8 bits for exponent. The default value of double-type is 0.0d while default value of float-type is 0.0f.
Example
public class DoubleFloatTest { public static void main(String []args) { double d = 55.637848675695785; float f = 25.657933f; System.out.println("Value of double: " + d); System.out.println("Value of float: " + f); } }
Output
Value of double: 55.637848675695786 Value of float: 25.657932
- Related Questions & Answers
- Format double type in Java
- Convert double primitive type to a Double object in Java
- Generate Random Float type number in Java
- Generate Random double type number in Java
- Convert a String to a double type number in Java
- Java Program to convert a String to a float type Number
- Can MySQL INT type be non-zero NULL?
- What happens when we type a URL?
- Round float and double numbers in Java
- Convert Masked Array elements to Float Type in Numpy
- Why is IPv6 preferred over IPv4?
- Float and Double in C
- Why is a Dictionary preferred over a Hashtable in C#?
- Boolean Type in Java
- Type Erasure in Java
Advertisements