• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

float and double!?!?

 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic