Float vs Double Java Example
Hello readers. In this tutorial we will learn about the float and double data types in the Java programming language. More specifically, we feature a comprehensive article about Float vs Double Java.
1. Introduction
In Java programming, float and double data types both represent in floating-point numbers but a double data type is a little more precise than float.
1.1 What is a Float data type?
The float data type in Java is a 32-bit single-precision type¬ and used when fractional precision calculations are required. It offers an unlimited range and has a default value of 0.0f
.
1.2 What is a Double data type?
The double data type in Java is a 64-bit double-precision type and used for decimal values like float. It offers an unlimited range and has a default value of 0.0d
.
1.3 Differences between Float and Double data types
Both float and double data types offer some important differences.
- Double data type offers more precision than float data type
- Double needs 64-bit storage compared to float 32-bit storage
- Double has a higher range than float data type as it has more bits to store the data
1.4 When to use Float and Double in Java?
Developers should use float if the application has memory constraints and numbers will fit in float range. If the number won’t fit in the float range, then use a double data type.
To start with the captioned tutorial, we are hoping that users at present have their preferred Ide installed on their machines. For easy usage, I am using Eclipse Ide on a Windows operating system.
2. Float vs Double Java Example
We’ll demonstrate the use of the float and double data types in Java. For a better understanding, developers can execute the below code in Eclipse Ide.
2.1 Example
Example.java
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package com.jcg.java; public class Example { public static void main(String[] args) { /* * float is 32-bit single precision type and used when fractional precision * calculations are required. * * Syntax: float = f; */ float floatVariable = 10 .4f; System.out.println( "Value of Float variable is= " + floatVariable); // 1. Create a Double object from the Double primitive type. double doubleVariable = 10.10 ; Double dObj1 = new Double(doubleVariable); System.out.println( "Value of Double variable is= " + dObj1); /* * 2. Create Double object from the String type. This method can * throws 'NumberFormatException' if String doesn't contain a parsable number. */ double dObj2 = new Double( "25.34" ); System.out.println( "Value of Double variable is= " + dObj2); } } |
If everything goes well, the following output will be printed in the Ide console.
Output
1 2 3 | Value of Float variable is= 10.4 Value of Double variable is= 10.1 Value of Double variable is= 25.34 |
That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!
3. Conclusion
In this tutorial, we learned the float and double data types in Java. Developers can download the sample application as an Eclipse project in the Downloads section.
4. Download the Eclipse Project
This was an example of float and double data types in the Java programming language.
You can download the full source code of this example here: Float vs Double Java Example
We created a good infographic on the difference between float and java, that you can add with this source: https://favtutor.com/blogs/java-float-vs-double