🌐
Reddit
reddit.com › r/learnprogramming › when should i use double and when should i use float for java?
r/learnprogramming on Reddit: When should I use double and when should I use float for Java?
January 6, 2024 -

I am learning Java, and am confuse on when to choose double or float for my real numbers or int. It feels like, it doesn’t matter because from my limited experience (with Java) both of them deliver the same results, but I don’t want to go further down the learning curve with Java and have a bad habit of using either messing up my code, and not having a clue as to why. So, when should you use float and double?

🌐
Reddit
reddit.com › r/java › double or float?
r/java on Reddit: Double or float?
August 31, 2018 -

Hello, I'm currently making a small matrix library in java, specifically for use with neural networks. So far I've done nearly all the functions and I used floats.

My question is if it would be better to use floats or doubles for this task, neural networks need precise values but are also very resource heavy.

Floats, doubles or write everything twice for overloading. Any ideas?

EDIT: Thanks for the answers, i finished version 1.0 today. If anyone wants to check it out feel free, just go here, hopefully it works well.

🌐
Reddit
reddit.com › r/learnjava › [new cs student]trouble differentiating between floats and doubles--why is 23.5 considered a double?
r/learnjava on Reddit: [New CS Student]Trouble differentiating between floats and doubles--why is 23.5 considered a double?
August 7, 2021 -

From my textbook:

When you write a floating-point literal in your program code, Java assumes it to be of the double data type. A double value is not compatible with a float variable because a double can be much larger or smaller than the allowable range for a float. As a result, code such as the following will cause an error:

Example 1:

float number;
number = 23.5;       //Error!

You can force a double to be treated as a float, by suffixing it with the letter F or f. The preceding code can be rewritten in the following manner to prevent an error:

Example 2:

float number;
number = 23.5F;       //This will work.

===================================================

I can't figure out why example 1 produces an error. Why is 23.5 considered a double? I thought it was a float.

AFAIK, float numbers are precise up to 7 decimal digits of accuracy. 23.5 only has 1 decimal digit. What am I not understanding? I thought by typing 'float number;' you are declaring the number variable to be a float. Why would you need the extra step of adding an F?

Thanks

🌐
Reddit
reddit.com › r/learnprogramming › float vs. double – what's the key difference?"
r/learnprogramming on Reddit: float vs. double – What's the Key Difference?"
November 2, 2024 - The difference is memory size. Floats in Java (which I assume is the language you’re talking about) take up 32 bits of memory, the same as an integer. Doubles, as you might guess from the name, take up double the space: 64 bits.
🌐
Reddit
reddit.com › r/explainlikeimfive › eli5: difference between double and floating point data types
r/explainlikeimfive on Reddit: ELI5: difference between double and floating point data types
May 17, 2014 - They can have a lot of digits after the decimal point in the number. The actual difference between them is in size. According to the Java Language Specification, a float is a 32-bit value, while a double is a 64-bit value.
🌐
Reddit
reddit.com › r/learnjava › why use int/float?
r/learnjava on Reddit: Why use int/float?
January 20, 2016 -

Why would someone even use an int or a float?

I ask this because while int does use less data, you risk overflows and also in general a double is more accurate. How significant is the amount of data saved actually that one would choose that over a double?

Top answer
1 of 5
5

How significant is the amount of data saved actually that one would choose that over a double?

While i think that one of the most important points for using an int is really that it is an integer, i.e. pure with no decimals (as others have pointed out), i also wanted to address this question.

A double is twice as big as an int or float. This will not be an issue in a small program you write for a class and it won't even be an issue in many industry-level applications, but there are certainly times where doubling the amount of data that you have to transmit for no gain whatsoever (if you don't happen to need the added precision of a double) can have a huge impact. At work we often deal with the transmission of tens or hundreds of gigabytes of data. Of course, this number wouldn't be exactly twice as large if we used doubles instead of wherever possible, but it would certainly grow a substantial amount, which would certainly influence transmission times noticeably.

It is certainly fine to generally worry more about things like code readability than performance or space requirements, but there are applications where you do need to worry about size. And i don't think getting into the habit of using double "just because" is a good thing.

2 of 5
2

Most of what is said is only half correct. Remember that int/float and other primitive are represented using x amount of bits. In the days that java started off with embedded systems with limited memory thus you had to constraint how much memory your program could use to do operations and how much memory it needed to use overall. This is where good use of primitive types comes in.

Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › difference-between-float-and-double
Difference between Float and Double - GeeksforGeeks
July 23, 2025 - The key difference is their precision and storage size. A float is typically a 32-bit number with a precision of about 7 decimal digits, while a double is a 64-bit number with a precision of about 15 decimal digits.
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Float-vs-Double-Whats-the-difference
Java double vs float: What's the difference?
The lower range of a double in Java is 4.9E-324. The upper range of a float in Java is 3.4028235E38.
Published: May 23, 2026
🌐
Scaler
scaler.com › home › topics › float vs double in java - difference you should know
Float Vs Double in Java - Difference You Should Know - Scaler Topics
March 27, 2024 - As the Float consumes less memory in comparison to the Double data type. By default, Java treats any decimal or float numbers as double type, so in case, if we want to define any value as float, we need to manually typecast it using the 'f' or 'F' keyword in the suffix of the number.
🌐
Blogger
javarevisited.blogspot.com › 2016 › 05 › difference-between-float-and-double-in-java.html
Difference between float and double variable in Java? Example
July 26, 2023 - Here are some key differences between float and double in Java : 1. The double data type is more precise than float in Java. 2. double-takes more space than float in Java. double needs 64-bit storage compare to 32-bit storage of float data type.
🌐
Reddit
reddit.com › r/learnprogramming › what is the difference between double and float
r/learnprogramming on Reddit: what is the difference between double and float
December 21, 2020 - I say this coz in majority of the tutorials i watched I no one mentioned doubles. only floats. I remember using doubles when i first started programming 3 years ago in java and c# but after that i started using floats
🌐
Baeldung
baeldung.com › home › java › java numbers › float vs. double in java
Float vs. Double in Java | Baeldung
January 4, 2025 - A float is a 32-bit single-precision floating-point type, occupying four bytes of memory. This compact size makes it well-suited for memory-constrained environments like embedded systems and mobile devices.
🌐
Blogger
javahungry.blogspot.com › 2019 › 11 › difference-between-float-and-double-in-java.html
7 Difference Between float and double Datatypes in Java | Java Hungry
Data loss is expected when double is converted to float. 5. Default datatype : float is not the default data type value of floating point literals. double is the default data type of floating point literals. 6. Wrapper class : Wrapper class for float is java.lang.Float.
🌐
Educative
educative.io › blog › float-vs-double-in-java
Float vs. double in Java
Precision and range: A float offers roughly 7 decimal digits of precision with a max range of approximately ±3.4×10³⁸, while a double provides about 15–16 decimal digits with a range up to approximately ±1.7×10³⁰⁸. Default type in Java: The compiler treats floating-point literals ...
🌐
Reddit
reddit.com › r/programming › float or double?
r/programming on Reddit: Float or double?
June 5, 2017 - Doubles are only slower in that they take more time to copy and you fit less of them in cache. I wrote a ray tracer from scratch with floats in fear of double computation time, and then had precision issues and converted to doubles.
🌐
Javatpoint
javatpoint.com › float-vs-double-java
Float Vs Double Java - Javatpoint
Java Program to swap two string variables without using third or temp variable.