• Post Reply Bookmark Topic Watch Topic
  • New Topic

int to byte -- why do I need to cast?  RSS feed

 
Smitty Werben
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading a book on Java and we are at a point where it is explaining type conversion in expressions.

One of the examples shared has a byte being multiplied by itself and then assigned back to itself

byte b;
b = 10;
b = (byte) (b * b);

this is all good and dandy (that is, the code functions properly).

However, I am confused why I need to typecast here! Without the cast, the compiler screams, "Type mismatch: cannot convert from int to byte." Yet I haven't converted to an int?? It appears there was an implicit conversion, yet I am blind to it.

The final value, 100, is clearly within byte's range of -127 to +127 isn't it? So I am lost as to what is the issue here. Please help!
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems like you expect the compiler to evaluate that expression and find out that it's 100. That might work fine in this case, but then there are more complicated cases. What if there were two variables involved and you can look and see the result? Four variables? Some array entries? And what if there could be another thread which could change the value of b? The possibilities are endless.

So the compiler writers decided to not be that clever. After all, it's hard enough to write a compiler as it is. They drew the line here: if the value is a constant, then the compiler knows what it is and can act accordingly. Otherwise, the compiler doesn't know what the value is and therefore it requires the cast from int to byte.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hay Smitty,

When you do mathematical operations on byte, Java do Widening( automatic type promotion) to byte(implicitly up casted) to integer this case. so when you perform

b = (byte) (b * b); // b*b will be evaluated as integer.

As b*b result is wider than byte so you need to downcast it to byte.

To remove compilation error.

Hope you understand ..

Here is the link :webpage

Thanks ,
Satya
 
Smitty Werben
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the welcome and the responses!


So the compiler is playing it safe and assumes the 'volume of data' could be larger than the byte sized 'cup', thus it automatically performs calculations at the next 'cup' size?

For instance, byte variables have int sized calculations so int variables would have long sized calculations? And float variables would have double sized calculations?

 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

For instance, byte variables have int sized calculations so int variables would have long sized calculations? And float variables would have double sized calculations?


What do you think ?
 
Smitty Werben
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S Majumder wrote:

For instance, byte variables have int sized calculations so int variables would have long sized calculations? And float variables would have double sized calculations?


What do you think ?


That is my guess. I am asking you, the expert, though.
 
S Majumder
Ranch Hand
Posts: 349
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Case :1 Here we need to cast the value to float



Case :2 Here we need to cast intnum+longnum to int


 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Smitty Werben wrote:For instance, byte variables have int sized calculations so int variables would have long sized calculations? And float variables would have double sized calculations?


That's a reasonable guess at how it works. But no. All calculations involving byte, short, char, and int values result in an int value. Your idea is more logical and symmetrical, I have to say, but in real life all of those int calculations are done in 32-bit hardware registers which is what an int value represents.
 
Smitty Werben
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, especially for that last post, Paul. Really helped me look at different examples and really get into it. So int and below are calculated as int. That's interesting.

I also had no idea += was handled differently! Thanks, Satya for that. It looks like there is an automatic cast applied there. But if one types out the assignment the long way, it does not (and just throws the error).

Little things like this I never learned in college!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!