954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

float declaration problem

I am having trouble compiling the following piece of code, it keeps giving me an error talking about an possible loss of precision. I know i could replace the one line to:
private float someNum = 0;
but i don't like that becuase it is assigning an int to a float ... yes yes i know that the compiler will use coersion and cast it off to a float, but is there a way around this?!?

class test 
{
   private float someNum = 0.0;
   public static void main(String[] args) {}
}
paradox814
Posting Whiz
351 posts since Oct 2004
Reputation Points: 13
Solved Threads: 4
 

Floating point literals in Java are always of type double unless you take care to tell the compiler they're of type float.
Double is a 64 bit floating point number, float is a 32 bit number.

Either change the type to double or change the initial value to 0.0f

And while you're at it follow the Java class naming conventions and
1) name your class in CamelCase, starting the name with a CAPITAL letter
2) use packages. While not a problem yet there are many uses where things don't work well unless you use a package.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You