Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why this type of Cast isn't allowed?  RSS feed

 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there ! Good day to y'all reading my post. (and to everyone in the ranch in general).

May I ask a little thing? I have some fundamental doubts about some Casting, and Boxing, and some other things[maybe-- ]

First of all, Why is this allowed by the compiler:



and this:



isn't?
I know I'm taking a risk here by asking this question because the answer is probably as obvious as the sourness of lemons, but still I need to know, please please...

Take care.
Jose
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A lot of things are only obvious right after you know them. Don't stop asking questions!

When you use casting you do things that are risky but possible. The compiler just wants your assurance that you've recognized the risk and decided to take it.

When the compiler finds a number of the right size and format like 800 it figures it's an int. The compiler knows how to move one number to another, but because this move will lose bits the compiler says it's an error. With casting you say "I know you think this is an int, but I'd like to trim it down to a byte. I know I might truncate some bits, but I'm taking the chance." Since you tell the compiler you know what you're doing, it compiles. Possible but risky.

Next you try to cast 800 to a Byte. Byte is a class, and things that we assign to a Byte variable must be compatible with that class. The compiler figures 800 is an int, which is not an instance of Byte. So even though you tell the compiler you want to take the risk of putting 800 into 8 bits, the compiler just doesn't know how to make a Byte variable point to an int. Not possible even if you take all the risk.

Did that make sense?
[ November 30, 2007: Message edited by: Stan James ]
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was probably one of the best explanations I've heard.

Justin Fox
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can either autobox or widen / narrow primitives, but not both.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amazing explanation indeed ! congratulations Mr. James, you'd be a great Java instructor if you wanted to. (If you aren't already )
Now, I would like to ask one more question relative to the same topic (I suppose)and would love If you could answer it in a similar fashion.

Here's some code:



Why is the byte type argument 'b' Not allowed for Method:
void method1(Long number) ?

Could someone please give me a detailed explanation? like the provided by Stan?

thanks in advance,
Jose
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said, you can autobox OR widen, but not both.

Automatic conversion from a byte to a long is not a problem.
Automatic conversion from a byte to a Byte is not a problem.

Now automatic conversion from a long to a Long is also not a problem. However, you can only do 1-step conversions like this. If you want to convert a byte into a Long, that would need two conversions - first from byte to long, then from long to Long.

Now the compiler is smart, but not smart enough to handle the 2-step conversion.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Nice explanation Sir, and the Best way to remember the lesson.
Thank you very much.
See you next time. (It will be sooner than anyone thinks)

Thank you Thank you Thank you Thank you Thank you Thank you Thank you

Good Luck,
Jose
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!