Consider the following piece of code:
class Converter {
public static void main(String args[]) {
byte num = 1;
num = num * 2.5;
System.out.println("Result is: " + num);
}
}
The compiler throws the following error:
error: incompatible types: possible lossy conversion from double to the byte at line 1
If I change the second line of the main() method and I use the *= shorthand operator:
class Converter {
public static void main(String args[]) {
byte num = 1;
num *= 2.5;
System.out.println("Result is: " + num);
}
}
The code compiles and runs successfully with the output:
Result is: 2
Why the *= shorthand operator is behaving differently from the full expression num = num * 2.5?

x = (byte)(x * y). So it does have an implicit type casting. I mean the first error is already warning you, that a type conversion will make you lose the preccision when casting fromdoubletobytejavap -v Converter.classto see whats happening in the second case. Pay special attention to the d2i and i2b instructions :)