File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Compiler Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compiler Error" Watch "Compiler Error" New topic
Author

Compiler Error

Mike Shn
Ranch Hand

Joined: May 26, 2001
Posts: 149
I declare variable as float point and got the compiler error
float f = 3.14;
Why it happened?
Thanks
Irene Loos
Ranch Hand

Joined: Apr 15, 2002
Posts: 78
It has to be declared this way:
float f = 3.14f;
Irene


Irene Loos
Mike Shn
Ranch Hand

Joined: May 26, 2001
Posts: 149
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

Joined: Apr 15, 2002
Posts: 78
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

Joined: Apr 15, 2002
Posts: 78
Sorry for the misspelling of "conversion". Check out this link
Java Language Specifications
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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 ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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

Joined: Apr 15, 2002
Posts: 78
Thank you, Dirk. I learned something today! I was wondering how to do the edits but never actually had time to look around.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compiler Error