aspose file tools*
The moose likes Beginning Java and the fly likes Float a=3 doesn't compile but Short a=3 does. Why ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Float a=3 doesn Watch "Float a=3 doesn New topic
Author

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

Daud Miyan
Greenhorn

Joined: Jul 07, 2010
Posts: 23

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

Joined: Aug 24, 2010
Posts: 35
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

Joined: Jul 07, 2010
Posts: 23
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

Joined: Mar 08, 2010
Posts: 222

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.


Personal page, SCJP 6 with 91%, SCWCD 5 with 84%, OCMJD
Daud Miyan
Greenhorn

Joined: Jul 07, 2010
Posts: 23
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

Joined: Mar 17, 2010
Posts: 1051

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

SCJP6.0,My blog Ranchers from Delhi
Elchin Asgarli
Ranch Hand

Joined: Mar 08, 2010
Posts: 222

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

Joined: Mar 17, 2010
Posts: 1051

i think ,this is what i say above
Elchin Asgarli
Ranch Hand

Joined: Mar 08, 2010
Posts: 222

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

Joined: Jul 07, 2010
Posts: 23
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

Joined: Oct 13, 2005
Posts: 38851
    
  23
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

Joined: Oct 13, 2005
Posts: 38851
    
  23
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

Joined: Mar 17, 2010
Posts: 1051

Thanks..Campbell Ritchie..
akhilesh bar
Greenhorn

Joined: Sep 10, 2010
Posts: 4
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

Joined: Mar 17, 2010
Posts: 1051

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Float a=3 doesn't compile but Short a=3 does. Why ?