This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi. I know all floating points are by default type double (unless you type f or F).
However, i was thinking... If i instantiate a very large number, it won't help me to insert an f at it's end, because it wouldn't work as a cast. In this case i'm forced to explicitly cast the number (what's the default behaviour when your "shrinking" the bits representation).
As i said in the post subject, this is just an observation. But, here's one thought. The point of inserting the (float) cast before the number is to say the compiler something like "Hey, i know what i'm doing, don't bother". In other hand, when you insert the F or f at the end of the number, you're telling the compiler "Hey, i want this as a float, not a double, ok". So, in a more abstract sense, this two things means the actual same thing. Then i ask myself if this whole thing is actually necessary...? Why float isn't just float, not mattering the f or F? Also with longs...? Is this because some IEEE standard or something?
[ January 27, 2005: Message edited by: Leandro Melo ] [ January 27, 2005: Message edited by: Leandro Melo ]
In my opinion, casting and appending an 'F' is not really the same. Casting means you are forcing the number to fit in 4 bytes. In other words, bits may be lost.
However, when you append an 'F', you are merely indicating to the JVM not to use double. It says nothing about bits truncation. The reason why the first line of code fails compilation is that appending an 'F' doesn't truncate extra bits, so you will get the "floating point too large" error.
You don't get a compilation error for the second line because the JVM will truncate the extra bits, so that only 4 bytes are left.
I do hope I'm making sense.
Current Status:<br /> <br />SCJP 1.4<br />SCJD (in progress)
Joined: Mar 27, 2004
I agree with you Liang. Append f or F doesn't tell the compiler to truncate the value. Just like appending L or l to a number. But one thing that i'd like to understand better is "why not avoiding this kinda stuff". Why couldn't we write
and the variable 'a' actually be a float (and not a double). So, my point here is not the casting or non-casting itself, but why do me make things more "complicated" than they really need to be. I'm not saying the appending an f or a l at the number is really "complicated", but is it really necessary? Maybe it's some kind of ieee standart or it has some very arguments to be this way... i don't know... that's what i wanted to know.
Joined: Jun 25, 2004
As you know, Java enforces the "default data type" rule. In other words, numbers hardcoded in the source code are integers by default etc. What I think is that Java has to enforce this rule in order to know how to handle certain arithmetic operations. In the end, it just happens that the developers have chosen to use double as default for floating point values, so they require programmers like us to provide hints to the compiler if we want any workarounds (e.g appending an 'F' to signify float).
Not sure if what I said here makes sense, but just a thought.