Why does Java support double data type while allowing float types to used as well? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Why does Java support double data type while allowing float types to used as well?

Java data types include float and double for fractions. However unlike other languages, java supports float but a bit differently by using 5.6f as values but assigning it the double type.

23rd Apr 2019, 11:39 AM
Dechen
4 Answers
+ 5
Unfortunately memory is not unlimited in computers.
24th Apr 2019, 2:19 AM
Sonic
Sonic - avatar
+ 4
Just to clarify your question: 5.6f is a literal value. Java assumes that when you write down a decimal value like 3.14 or 1.23e6 you mean a double type, You must instruct the compiler that you want a float type by typing an 'f' suffix to the value, and only in that case will Java accept the assignment. As for the need for both types, it comes down to memory space. If you are programming a 3d game, for example, with millions of vertices, points etc, you will be more than ready to sacrifice some precision for the 50% decrease in memory usage.
23rd Apr 2019, 1:53 PM
Csaba Varga
Csaba Varga - avatar
+ 3
Both Double and float data type are used to represent floating point numbers in Java, a double data type ismore precise than float. A double variable can provide precision up to 15 to 16 decimal points as compared tofloat precision of 6 to 7 decimal digits
23rd Apr 2019, 11:42 AM
Alessio Benvenuti
Alessio Benvenuti - avatar
+ 2
There is not much difference except the size, if u want to use small data use float, that will just save a bit of space. But afterall it doesn't matter at all because u don't even save around 1KB.
23rd Apr 2019, 12:36 PM
Prerak Gada
Prerak Gada - avatar