Primitives cannot be cast the same way reference variables can. Maybe you are talking about primitive type conversion? Or maybe you are trying to cast between the wrapper types Float and Long?
If you are trying to do the latter, that is impossible because none of those two classes inherits from the other.
Maybe it would help if you showed us some code showcasing the problem?
Primitives cannot be cast the same way reference variables can. Maybe you are talking about primitive type conversion? Or maybe you are trying to cast between the wrapper types Float and Long?
If you are trying to do the latter, that is impossible because none of those two classes inherits from the other.
Maybe it would help if you showed us some code showcasing the problem?
Well im working on some past papers and this question came up:
Long v1 = 10L;
Float v2 = 10.3F;
Observe what happens when you remove the L and F. Rewrite these lines using casting.
so so far i put this but no luck due to it saying they cannot be casted:
Here you are trying to cast a reference of type Long to Float.
Note that to be able to successfully cast a reference to a type T, the object referred to by said variable must be of a subtype of T (or of T itself). The compiler is clever enough to see that this will never be the case for Float and Long, and does not allow the code to compile.
Ole Sandum wrote:Here you are trying to cast a reference of type Long to Float.
Note that to be able to successfully cast a reference to a type T, the object referred to by said variable must be of a subtype of T (or of T itself). The compiler is clever enough to see that this will never be the case for Float and Long, and does not allow the code to compile.
Not using the wrapper classes, no. But it is possible the paper used the primitive types long and float (lowercase l and f), which can be converted between eachother. Are you sure you quoted the paper directly without changing the case of those letters?
Ole Sandum wrote:Not using the wrapper classes, no. But it is possible the paper used the primitive types long and float (lowercase l and f), which can be converted between eachother. Are you sure you quoted the paper directly without changing the case of those letters?
100% quoted correctly, however it does say what happenes when F and L are removed however that should still present an invalid answer?
You can not cast reference types to another type that is not a sub- or super-type.
But that's not what your initial question was about. You mentioned floats and longs. Those are primitive types, not reference types. However, your code uses Float and Long, and those are reference types.
You'll find that casting between float and long will be no issue.
Post by:autobot
It will give me the powers of the gods. Not bad for a tiny ad:
a bit of art, as a gift, the permaculture playing cards