Hi there, I am just beginning to learn java right now, and have a real begeinner question. I ahve read that when you declare a float variable and ssigna value to it the number has to be followed by an "f". What I coudln't find out is why: var floatNumber = 12.5f;
What happened if I omit the "f" at the end?
the 12.5 will be a double constant, and Java will not automatically convert a double to a float becuase that looses precision. You will get an error "Type mismatch: cannot convert from double to float"
(ps: You can also use float ff = 1; because Java will automatically convert an int to float)
In the Lava Language Spec - the definitive description of the Java Language - the words constant and literal are used for two related but different meanings:
a "constant" is a field whose value cannot be changed
a "literal" is a value; it has no name, it just is what it is.
So in the code final double PI = 3.14159;
PI is a constant, and 3.14159 is a literal.
Oh dear! You caught me out there! I was being sloppy with my terminology and yes, I should have said literal. (In my defense we were discussing float vs double there, it was only later that we got onto constant vs literal.)
You're right. My fault. :)