Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Abhilash's Site ( Explicit Cast Is Not Needed in Some Cases ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Abhilash Watch "Abhilash New topic
Author

Abhilash's Site ( Explicit Cast Is Not Needed in Some Cases )

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
There are two mock questions at Abhilash,s angelfire web site:
1. byte b = 0;
b += 1; ( no explicit cast )
2. int i = 10;
byte k = i; ( no explicit cast )
do not cause compile error. I cannot figure out the underlying reason. Could these two cases be explained?
Bharatesh H Kakamari
Ranch Hand

Joined: Nov 09, 2000
Posts: 198
1. byte b = 0;
b += 1; ( no explicit cast )
Yes true, this does not require explicit casting. But b = b+1; requires explicit casting.
2. int i = 10;
byte k = i; ( no explicit cast )
This requires explicit casting
Bharatesh H Kakamari
Ranch Hand

Joined: Nov 09, 2000
Posts: 198
Again ...
byte k = 10; does not require explicit casting as 10 fits in the byte range.
HTH
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
I would like to know the reason why converting from int to byte does not need explicit casting. I only know how to follow the rule. I don't know any exceptions. Please help.
Kathy Rogers
Ranch Hand

Joined: Aug 04, 2000
Posts: 103
Basically, you have to cast an int to a byte almost all the time. The only time you don't have cast an int to a byte is when you're assigning a value to a byte variable and that value is something constant that is within a byte's range - basically if you give it a valid literal value (byte b = 10 ; ) or if you set it equal to a constant with a valid value (final int i = 10; byte b = i ; ). At compile time, the compiler can work out that you're assigning a valid value to the byte and so it doesn't complain. Doing this
int i = 10;
byte b = i;
won't work cos all the compiler will know is that i is a variable with a range much bigger than a byte's. It won't know the value that i will have when you run the program - even though it's obvious to you.
For arithmatic operations, Java usually promotes all the operands to ints (or bigger) so byte b = i *j; will cause problems even if i and j are bytes and the answer is within a byte's range. However there's one exception to this, and I don't know why, that is the ++ and -- operators. There's no automatic promotion there so
byte b = 1;
b++;
b--;
--b;
++b;
will work fine. However, you still have to be careful. Try
byte b = 127;
b++;
to see what I mean.
Hope this helps,
Kathy
[This message has been edited by Kathy Rogers (edited November 28, 2000).]
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Thank you, Kathy. You have turned my stupid question into an interesting one.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Abhilash's Site ( Explicit Cast Is Not Needed in Some Cases )
 
Similar Threads
expression involving int or smaller
How can it be valid?
++ / -- convert to int RHE chap 4
What happens to sign bit if int is 32 bits?
char addition