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?
thanks
Violet_82 89 Posting Whiz in Training
Recommended Answers
Jump to Post"var" isn't Java, but anyway, in Java if you omit the "f", eg 12.5, it's assumed to be a double (same as float but twice as many bits for much greater accuracy).
Jump to PostYes, because if you code
float ff = 12.5;
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 …
Jump to PostThe Oracle Java Tutorials are unbeatable, although they do require you to read carefully and think about it. Here's the one on data types:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Jump to PostIn 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 …
All 13 Replies
NormR1 563 Posting Sage Team Colleague
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
Violet_82 89 Posting Whiz in Training
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
Violet_82 89 Posting Whiz in Training
NormR1 563 Posting Sage Team Colleague
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
Violet_82 89 Posting Whiz in Training
NormR1 563 Posting Sage Team Colleague
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
Violet_82 89 Posting Whiz in Training
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
Violet_82 89 Posting Whiz in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.