File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes float and double observation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "float and double observation" Watch "float and double observation" New topic

float and double observation

Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
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 ]

Leandro Melo
SCJP 1.4, SCWCD 1.4
navneet shrivastava
Ranch Hand

Joined: Jul 09, 2004
Posts: 39

nice question... we can call it a issue rather.
still hunting for explanation.


Liang Anmian
Ranch Hand

Joined: Jun 25, 2004
Posts: 119
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)
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
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.
Liang Anmian
Ranch Hand

Joined: Jun 25, 2004
Posts: 119
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.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: float and double observation
Similar Threads
Float Primitive
Float a=3 doesn't compile but Short a=3 does. Why ?
Assignment to float
Can static methods cause any kind of overhead?