Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Float a=3 doesn't compile but Short a=3 does. Why ?

 
Daud Miyan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This throws up an error. It should have widened 3 to float and autoboxed it to a Float reference. What's wrong ?

But this doesn't throw any error. Why ?
 
Harnoor Singh
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What made you conclude that it should widened to float first? How come float (primitive type ) comes into picture in Float a = 3?
 
Daud Miyan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harnoor Singh wrote:What made you conclude that it should widened to float first? How come float (primitive type ) comes into picture in Float a = 3?

Since the LHS and RHS should have compatible types, the only way an int would be compatible with Float would be by widening itself to float and wrapping itself inside a Float wrapper. The RHS and LHS then will have the same type.

I may be wrong. What's the correct reason ?
 
Elchin Asgarli
Ranch Hand
Posts: 222
Chrome Eclipse IDE Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact the first reaction of JVM is correct, it should not widen and and then box. However the fact that it can wrap to short is a bit unclear to me. But my guess is that when you write "3", it implicitly casts it to short, thus there is no widening.

Page 254 from K&B book :
As we can see, you can successfully combine var-args with either widening or
boxing. Here's a review of the rules for overloading methods using widening, boxing,
and var-args:
- Primitive widening uses the "smallest" method argument possible.
- Used individually, boxing and var-args are compatible with overloading.
- You CANNOT widen from one wrapper type to another. (IS-A fails.)
- You CANNOT widen and then box. (An int can't become a Long.)
- You can box and then widen. (An int can become an Object, via Integer.)
- You can combine var-args with either widening or boxing.

 
Daud Miyan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks lot. Why 3 can be assigned to Short is explained here :
http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#184206
As to why Float a=3 won't work, the reason I guess is in the points that you mentioned. Widening from int to float followed by boxing is not allowed.
Thanks again
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct answer is:-

if you want to assign a floating-point literal to a variable of type float (32 bits), you must attach the
suffix F or f to the number. If you don't, the compiler will complain about a possible
loss of precision, because you're trying to fit a number into a (potentially) less precise
"container." The F suffix gives you a way to tell the compiler, "Hey, I know what I'm
doing, and I'll take the risk, thank you very much."


source :--Same from the K & B Book
 
Elchin Asgarli
Ranch Hand
Posts: 222
Chrome Eclipse IDE Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shanky Sohar wrote:Correct answer is:-

if you want to assign a floating-point literal to a variable of type float (32 bits), you must attach the
suffix F or f to the number. If you don't, the compiler will complain about a possible
loss of precision, because you're trying to fit a number into a (potentially) less precise
"container." The F suffix gives you a way to tell the compiler, "Hey, I know what I'm
doing, and I'll take the risk, thank you very much."


source :--Same from the K & B Book


Actually F suffix gives you a way to tell compiler "hey, this is a float, not double as you understand it by default". This is since all decimal numbers ( 3.2, 5.55, etc.) default to double in Java, and numbers default to int.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think ,this is what i say above
 
Elchin Asgarli
Ranch Hand
Posts: 222
Chrome Eclipse IDE Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shanky Sohar wrote:i think ,this is what i say above


And I gave my 2 cents to clarify the issue a bit more, which could be helpful for a newbie (and not only).
 
Daud Miyan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shanky Sohar wrote:Correct answer is:-

if you want to assign a floating-point literal to a variable of type float (32 bits), you must attach the
suffix F or f to the number. If you don't, the compiler will complain about a possible
loss of precision, because you're trying to fit a number into a (potentially) less precise
"container." The F suffix gives you a way to tell the compiler, "Hey, I know what I'm
doing, and I'll take the risk, thank you very much."


source :--Same from the K & B Book


Thanks a lot to everyone. Actually, what I was asking was WHY didn't 3 got promoted to float and assigned to a (of type Float), NOT that how can I make the statement work. Sorry for any confusion.

Thanks again.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the thing about the terminal f adds more confusion than it removes. If you write 3, it is implicitly an int; if you want a float, you must write (float) 3 or 3f. The latter (3f) is better, as it already is a float with the value of 3.0 in 32 bits. So the 3f version ought to compile. But it is no longer an int, so there is no question about widening and boxing. It is a boxing conversion plain and simple.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also think
The F suffix gives you a way to tell the compiler, "Hey, I know what I'm doing, and I'll take the risk, thank you very much."
applies to casting rather than the f suffix. The f suffix tells the compiler and the JVM:

"This is a float."
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks..Campbell Ritchie..
 
akhilesh bar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I second the reasoning of Elchih Asgarli... in case of short the literal 3 (int by-default) will be implicitly cast by the compiler , but in case you are initializing a float variable .. you will have to specify the compiler that its a floating type value by adding the suffix f to the literal 3 (float f = 3f;) , otherwise compiler will take 3 as a double. And the other complex method is explicit casting
(float f = (float) 3; )

am i right Seniors....???
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i expect you to spend some time here
what you are saying is partially correct.

try to Understand the meaning of lossy cast.if not able to grab it ask here but first try yourself.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic