
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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 Articles
- 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
- Round float and double numbers in Java
- Convert Masked Array elements to Float Type in Numpy
- Can MySQL INT type be non-zero NULL?
- Can we overload a method based on different return type but same argument type and number, in java?
- Float and Double in C
- Why is a Dictionary preferred over a Hashtable in C#?
- Comparison of double and float primitive types in Java\n
- Why is IPv6 preferred over IPv4?
- Boolean Type in Java

Advertisements