File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Why this type of Cast isn't allowed? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Why this type of Cast isn Watch "Why this type of Cast isn New topic
Author

Why this type of Cast isn't allowed?

Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
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

Joined: Jan 29, 2003
Posts: 8791
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 ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
That was probably one of the best explanations I've heard.

Justin Fox


You down with OOP? Yeah you know me!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

You can either autobox or widen / narrow primitives, but not both.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
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

Joined: Oct 27, 2005
Posts: 19216

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

Joined: May 28, 2007
Posts: 339
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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Why this type of Cast isn't allowed?
 
Similar Threads
Easy Queries
confusing stuff in wrapper classes
byte problem
Byte
Problem with Classes and Interfaces