How to handle automatic type promotion in expressions in Java?

In Java, type promotion happens automatically. For Example:

byte a = 85;

a = a * 8;

For this expression Java does an automatic type promotion from byte to int for handling large values which would be the result of the calculation. Now, when the calculation is done in int it can’t be assigned back to back. Thus, the above expression will return an error. In this case a casting is required to get the desired result.

byte a = 85;

a = (byte)(a * 8);

Leave a Reply