1 2 Previous Next 21 Replies Latest reply: Mar 16, 2009 5:33 PM by 807588

# f suffix for floating point.

Okay, I'm a proficient c++ programmer and have been learning Java for only a few weeks now.

I have a question about the f suffix for floating point varibles such as float f = 3.14f;

The f suffix casts this as float right? which is the same as float f = (float) 3.14; Correct?

Why do we have to add the f suffix in the first place? Doesn't the compiler know that we want a float and not a double? (single-precision 32-bit instead of double precision 64 bit) I really do not understand the concept here or why they need the f suffix.

Can someone explain?
• ###### 1. Re: f suffix for floating point.
I think Java and C++ agree on this, so I'm surprised by your question. The "f" suffix isn't a cast, it's part of the literal. And it's needed because programming languages are typed bottom-up, not top-down.
• ###### 2. Re: f suffix for floating point.
I understand what a float is and how it works and everything. I still don't understand the suffixing the F. Could you explain further?
• ###### 3. Re: f suffix for floating point.
Do you understand that C++/Java has floating point types float and double, and that each has their own literals?
• ###### 4. Re: f suffix for floating point.
I guess I dont' understand that. =\
• ###### 6. Re: f suffix for floating point.
Ok, I guess I've, embarrasingly enough, never touched floating litterals in C++. =\

I'm going to look them up now, but could you summarize it?
• ###### 8. Re: f suffix for floating point.
Thanks man, I guess I have some more studying to do. =)
• ###### 9. Re: f suffix for floating point.
So a questions...and this might be totally off the wall. Call me crazy. =) but a but you can create a float of the litteral type float or double? and double of the type float or double? Ha, that makes no sense to me. Can someone explain? I'm still very confused.
• ###### 10. Re: f suffix for floating point.
I think you need to read this, too: [http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2]
• ###### 11. Re: f suffix for floating point.
I'm still not quite getting it. =/

The f suffix denotes that the literal is of a floating-point type. Java has two different types of floating-point numbers. The type double is the default type.

The float type can have a double and a float literal. Is this true or false?
• ###### 12. Re: f suffix for floating point.
ThePHPGuy wrote:
The f suffix denotes that the literal is of a floating-point type.
Yes. The d suffix does the same.
Java has two different types of floating-point numbers.
Right.
The type double is the default type.
Right.
The float type can have a double and a float literal. Is this true or false?
No. At least not in any way I understand it.

I think you're confusing two things:

"floating point number" is any number in the IEEE floating point format.

"float" is a datatype holding a 32bit floating point number.
"double" is a datatype holding a 64bit floating point number.

floating point number literals can be either double literals (without suffix or if the "d" suffix is used) or float literals (when the "f" suffix is used).
• ###### 13. Re: f suffix for floating point.
You are making something simple, complicated.

1. The floating-point types are float and double
2. The following are literals of type double
``````3.14
3.14d``````
3. The following is a literal of type float
``3.14f``
• ###### 14. Re: f suffix for floating point.
So why in the world do we need to put the f or d after a decimal.
``float f = 456.78f;``
why can't we just write
``float f = 456.78;``
The compiler can't figure out we want a float instead of a double?

So, technically it's almost like casting it.
1 2 Previous Next