Forums Register Login

casting question

+Pie Number of slices to send: Send
I noticed that I can't cast a Float to an int, but that if I multiply the Float by an int, then I am allowed to do the cast. I'm wondering what is going on in the Java logic that accounts for this.


The best I can make out is that an operation using a Float creates a float value, not another Float, unless you assign the result to a Float. I'm guessing this generalization works for all primitive Wrappers?

I tried using instanceof as a test, and got two objections from the compiler:

I'm studying for the Java Associate Certification, and want to be sure I understand this. The question didn't come from a practice test, but some coding I'm doing. However, it seems like the tricky sort of stuff that shows up in the practice tests. Usually casting is between objects where one "is a" subclass of another. But having primitives involved is confusing me. How does the "is a" relationship pertain there?
+Pie Number of slices to send: Send
 

Phil Freihofner wrote:I noticed that I can't cast a Float to an int, but that if I multiply the Float by an int, then I am allowed to do the cast. I'm wondering what is going on in the Java logic that accounts for this.


The best I can make out is that an operation using a Float creates a float value, not another Float, unless you assign the result to a Float. I'm guessing this generalization works for all primitive Wrappers?

I tried using instanceof as a test, and got two objections from the compiler:



I'm studying for the Java Associate Certification, and want to be sure I understand this. The question didn't come from a practice test, but some coding I'm doing. However, it seems like the tricky sort of stuff that shows up in the practice tests. Usually casting is between objects where one "is a" subclass of another. But having primitives involved is confusing me. How does the "is a" relationship pertain there?




You can use to return only the integer part of the float primitive. Infact if you see JLS, all Wrapper classes have these xxxValue() apis. Does that solve your problem Phil?
+Pie Number of slices to send: Send
 

Phil Freihofner wrote:I noticed that I can't cast a Float to an int...


Phil,

Please DontWriteLongLines, even in comments. I've fixed it this time, but it makes your thread very hard to read.

@Mansukhdeep: Please don't copy (or quote) code with long lines for the same reason.

Thanks

Winston
+Pie Number of slices to send: Send
 

Phil Freihofner wrote:The best I can make out is that an operation using a Float creates a float value, not another Float, unless you assign the result to a Float. I'm guessing this generalization works for all primitive Wrappers?


Pretty much, but there are some gotchas.

In this case, f (your Float) is first unboxed to a float, and the result of float * int is a float.

I tried using instanceof as a test, and got two objections from the compiler:


Because instanceof requires an object as its left-hand operand; and primitives are not objects.

having primitives involved is confusing me. How does the "is a" relationship pertain there?


It doesn't, for the reason given above.

Winston
+Pie Number of slices to send: Send
Thanks to both Winston and Mansukhdeep!
My apologies for the "long lines".
Legend has it that if you rub the right tiny ad, a genie comes out.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 950 times.
Similar Threads
Why Byte boxing doesn't work here
float contrast
Using == operator on wrapper classes
Arithmetic promotion
primitive & primitive ref var : assignments
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 03:04:03.