• Post Reply Bookmark Topic Watch Topic
  • New Topic

Casting question???  RSS feed

 
orry kaplan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to cast a float to a long or a long to a float but every time java says i cannot cast them either way i try. Can they be casted?
 
Ole Sandum
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, orry.

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?
 
orry kaplan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ole Sandum wrote:Hi, orry.

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:

 
Ole Sandum
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
orry kaplan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


So basically it cannot be done?
 
Ole Sandum
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
orry kaplan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Stephan van Hulst
Saloon Keeper
Posts: 7820
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!