File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "float and double!?!?" Watch "float and double!?!?" New topic
Author

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
Sheriff

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: http://aspose.com/file-tools
 
subject: float and double!?!?