• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assignment to float

 
Fabricio Sanchez
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Fabricio Sanchez
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not completely true, Jeff. The default type of floating-point literals is double.
 
Jeff Bosch
Ranch Hand
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 150
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 150
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every expression in Java has a type. The type for "45e7" is double as this example shows.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic