wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assignment to float Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assignment to float" Watch "Assignment to float" New topic
Author

Assignment to float

Fabricio Sanchez
Ranch Hand

Joined: Mar 11, 2003
Posts: 33
I've come across this as an illegal assignment to float:

The explanation was that 42e7 is a double. Why is it a double? Does it not need a decimal point to be a double?
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Hi, Fabricio -
All floating-point numbers are double in Java unless you specifically make them a float:

Notice the 'f' at the end, which tells the compiler to make this a float. By excluding the 'f', you're telling the compiler to assign a double value to a float variable, and the compiler complains because of the possible loss of precision. By doing it this way, you're telling the compiler that it's okay to assign this value to a float.
Also, I recommend using descriptive names for variables. Makes code much easier to read.
Hope that helps!


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Fabricio Sanchez
Ranch Hand

Joined: Mar 11, 2003
Posts: 33
Thanx Jeff! That helps! I didn't realise that scientific notation (e.g. 42e7) implied a floating point number (and so defaults to double).
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
It's not the notation that makes it a double, it's the attempt to assign it to a variable of type float. According to the specs, any operation that involves a floating type converts the operation to a floating type.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Here's what happens when we assign this value to an int and a float:

The result:

Notice how the float value has the decimal point added by the JVM, because it's implied by the variable being of type float.
Hope that helps!
[ October 24, 2003: Message edited by: Jeff Bosch ]
Doug Dunn
Author
Ranch Hand

Joined: Aug 03, 2003
Posts: 66
That's not completely true, Jeff. The default type of floating-point literals is double.


Download a copy of <a href="http://www.javarules.com" target="_blank" rel="nofollow"><i>"Mastering The Fundamentals of The Java Programming Language"</i></a>
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Hi, Doug -
That's not completely true, Jeff. The default type of floating-point literals is double.

I'm not sure to what you're referring. I do say that the default type is double, which is one of the two floating-point types.
Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
Hi Jeff
It's not the notation that makes it a double, it's the attempt to assign it to a variable of type float.

I don't completely agree with the above statement. In the following assignment expression:
float f = 1; // This is an attempt to assign 1 to a variable of type float
The initializer 1 is an int type and not a double type.
At the same time
float f = 1.0; // wont' compile
Hope you agree?
Thanks
Harwinder
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
If I use the assignment

as suggested, this is the output:

That's a floating point number, not an integer. While it's true that "1" is an int, it has to be converted to a floating point number when assigned to a type float.
The second assignment,

won't compile because floating point numbers are double by default. You'd need to change that to:

Have a great weekend! I'm outta here!
Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
I agree with everything you just said but I still don't agree with your statement:

It's not the notation that makes it a double, it's the attempt to assign it to a variable of type float.

It doesn't make it "double".
Peace.
Cheers
Harwinder
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Technically, probably true: the compiler probably makes a copy of the literal initializer, the copy being of type double, then assigns that (if it can) to the space allocated for the memory addressed by the variable of type double or float, whichever type the variable is. Looking at it this way, the original initializer is not changed, but it's not really used, either.
This processing would for all practical purposes be invisible to the programmer.
Perhaps I'm missing the reason for your disagreement?
Have a great weekend!
[ October 24, 2003: Message edited by: Jeff Bosch ]
Doug Dunn
Author
Ranch Hand

Joined: Aug 03, 2003
Posts: 66
I missed your first post, Jeff. "My bad." I'm starting to get lazy answering these things. Fortunately, I only have one more day to go.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Every expression in Java has a type. The type for "45e7" is double as this example shows.


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assignment to float
 
Similar Threads
See this question
Why is "float f4 = 42e7" not considered a valid declaration of a float in Java?
double type
literal question
float and double range