File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Why do I need F after number if I've already defined variable as float? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why do I need F after number if I Watch "Why do I need F after number if I New topic
Author

Why do I need F after number if I've already defined variable as float?

Keith Alexander
Greenhorn

Joined: Oct 23, 2012
Posts: 8
Sorry for the super simple question; I'm teaching myself java. The chapter talks about declaring variables as float or double. But it also writes that I need to put an F after the number or it is assumed a double.

e.g.

float pi = 3.14F;

Is there a valid reason for needing the F or is it a legacy compiler issue or something? I'm not understanding why it's necessary if I've already declared a variable as float? (It implies the declaration is redundant or ignored).

Thanks.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

What you declare it as doesn't matter. There are a couple of separate, independent steps. One of those steps is to evaluate the literal 3.14. The rules of the Java language says that the type of a floating point literal without an f or F after it is double. This is probably because floats are almost never used, so it keeps the code less cluttered to let double be the default rather than float.

Another step is to check if the expression on the RHS can be assigned to the variable on the RHS. The compiler sees that the type of the expression on the RHS is double, and that's all that matters to it. It sees that the LHS is float, and it knows that if you cram a double into a float, you may lose precision, so it complains.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

The variable declaration has nothing to do with the type of the literal. Even though the variable is declared as a float, the literal is interpreted independently, and in place. Only when the assignment is evaluated do the semantics of the statement apply.

Edit: 20 seconds too late!


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18760
    
  40



As a side note, Java does have special handling when the RHS expression is a constant. This handling allows int values to be assigned to byte variables, for example.... You can actually make an argument of why the designers decided to exclude this handling for longs, floats, and doubles.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Keith Alexander
Greenhorn

Joined: Oct 23, 2012
Posts: 8
Thanks for your help. It sort of makes sense but the logic of having both LHS and RHS declarations that can conflict with each other still a little confusing (I'm thinking too literally I guess). I'll keep plugging on and see if it sinks in a little more. Thanks again.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Keith Alexander wrote:Thanks for your help. It sort of makes sense but the logic of having both LHS and RHS declarations that can conflict with each other still a little confusing (I'm thinking too literally I guess). I'll keep plugging on and see if it sinks in a little more. Thanks again.


The idea is to keep things simple. For a language definition and a compiler implementation, things are simpler if we keep stuff like this decoupled, and don't have special cases. So the rules are:

1. A FP literal with no trailing F is a double. Always. (The always makes it simple, otherwise we have to check for special cases like how it's being used.)

2. If the RHS is of a type that cannot be assigned to the LHS without losing precision, it's an error.
2.1 Special case: Even if the type of the RHS is not compatible with the type of the LHS, as long as the RHS is a compile-time constant whose value does happen to fit into the LHS, allow it (for some types).

Rule 1 and rule 2 are independent of each other, which helps keeps things simple.

Rule 2.1 is a special case. We try to avoid those, as they complicate things, but in this case, it is worth it, because it saves of from having to cast when we do things like


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why do I need F after number if I've already defined variable as float?