I'm working through "Beginning Java Programming" by Bart Baesens, Aimee Backiel, and Steppe vanden Broucke.
And I'm talking about the difference between appending and not appending a letter when assigning a literal to an identifier. Ex:
float ratio = 0.234
compared to:
float ratio = 0.234F
On the page I'm looking at (and I can't see clarification anywhere else in the book) it's not helpful at all.
Googling results in many not-so-clear answers. I did find the following answer on Quora:
"
When the interpreter sees float ratio = 0.234;
it knows what float means because its a reserved keyword
it infers that ratio is an identifier
= is an assignment operator
0.234 is a double literal and
; is a delimiter
because that's just how the language has been defined. So the statement is syntactically correct but when the semantic check begins it sees that you are trying to store a 64-bit double literal into a 32-bit float variable which will lead to loss in precision so it will give you an error or a warning.
"
But why would doing float ratio = 0.234
make it think I want to assign a 64-bit double literal into a 32-bit float variable? I mean. I'm literally telling it that I want a float, so there must be another reason that I'm missing.
The 32/64 discussion here relates to the literal, not the variable.
0.234F
is a 32bit float literal, while 0.234
is a 64bit double literal.
So if we reexamine those two statements:
float ratio = 0.234F
means "take the 32bit float value of 0.234 and place it in the variable ratio
".
float ratio = 0.234
means "take the 64bit double value of 0.234 and place it in the variable ratio
" - which, as you've seen, is not possible and results in a compilation error.