• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

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

 
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again ...
byte k = 10; does not require explicit casting as 10 fits in the byte range.
HTH
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Kathy. You have turned my stupid question into an interesting one.
reply
    Bookmark Topic Watch Topic
  • New Topic