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 PostThat doesn't look like valid java code. var is not part of java.
What happened if I omit the "f" at the end?
If it is java, compile and execute the program and see what happens.
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 tutorial has some info:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
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.