This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes float and double!?!? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "float and double!?!?" Watch "float and double!?!?" New topic

float and double!?!?

Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
1 --> float f1 = 3.14; //error
2 --> float f2 = 3.14f; //ok
3 --> double d1 = 3.14; //ok
4 --> double d2 = 3.14d; //ok
why the first example is wrong???
why we don't need to put 'd' in example 3???
when do I need to use 'd' after numbers for doubles???
Jessica Sant

Joined: Oct 17, 2001
Posts: 4313

This quote should help you out with your question...
Quoted from Sun Certified Programmer & Developer Study Guide by Kathy Sierra and Bert Bates
Floating-point literals are defined as a double (64 bits) by default, so if you want to assign a floating-point literal to a variable of type float(32 bits), you must attach the suffics F or f to the number. If you don't, the compiler will complain about a possible loss of precision, because you're trying to fit a number into a (potentially) less precise 'container'. The F suffix gives you a way to tell the compiler, "Hey, I know what I'm doing and I'll take the risk, thank you very much."

[ January 08, 2003: Message edited by: Jessica Sant ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
I agree. Here's the link:
subject: float and double!?!?
It's not a secret anymore!