I`m learning Java with the Herbert Schildt book's: Java a Beginner's Guide. In that book appears this code:
// A promotion surprise!
class PromDemo{
public static void main(String args[]){
byte b;
int i;
b = 10;
i = b * b; // OK, no cast needed
b = 10;
b = (byte) (b * b); // cast needed!!
System.out.println("i and b: " + i + " " + b);
}
}
I don't understand why I must use (byte) in the line:
b = (byte) (b * b); // cast needed!!
b was defined as a byte and the result of b * b is 100 which is right value for a byte (-128...127).
Thank you.