• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Compiler Error

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I declare variable as float point and got the compiler error
float f = 3.14;
Why it happened?
Thanks
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has to be declared this way:
float f = 3.14f;
Irene
 
Mike Shn
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is must be declare float f = 3.14f?
Why int = 5; delcares like that not like int = 5i;?
[ June 04, 2002: Message edited by: Mike Shn ]
 
Irene Loos
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to Java Language specifications (http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#230798) float must be declared this way. The way how you used it, you have been trying to perform conversation from double to a float (narrowing) without a cast. Compiler does not allow this.
I hope this helps.
Irene Loos
 
Irene Loos
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the misspelling of "conversion". Check out this link
Java Language Specifications
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reading parts of the JLS (as Irene has suggested) from time to time is strongly recommended. That's where the rules of the the Java language are found.
Mike, the short answer to your query is, floating point literals in Java (numbers like 1.2 , 0.1 , 0.0001 , etc.) are by default of type double. If you don't want that much precision and want to use the data type float instead, then you must explicitly tell the compiler of your desired use. Otherwise, it's going to error at compile time, essentially asking you if you are certain that you will accept the possible loss of precision if you try to assign a floating point literal to an identifier of type float.
Integer literals are by default of type int. Sometimes, when you want an integer literal to actually be used as a byte or a char or a short or a long, then you must explicitly tell the compiler of your wishes.
Note that you don't have to explicitly tell the compiler to use a floating point literal as a double or an integral literal as an int because these are the default data types.
Making any sense?
[ June 05, 2002: Message edited by: Dirk Schreckmann ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S. Irene, you can edit your posts if you'd like. Take a look at the various icons above each post. The one that looks like a piece of paper with the pencil pointing at it is the "Edit/Delete Post" icon. Thanks for all the nice posts. Hope to see you 'round the Ranch!
 
Irene Loos
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Dirk. I learned something today! I was wondering how to do the edits but never actually had time to look around.
 
reply
    Bookmark Topic Watch Topic
  • New Topic