Java - Operator Multiplication Operator *

What is Multiplication Operator?

The multiplication operator (*) is used in the form

  
operand1 * operand2 

Multiplication Operator Promotion

20 * 6 will be replaced by 120, b2 = 120 is ok, because 120 is in the range -128 and 127

byte b2 = 5; 
b2 = 20 * 6; // Ok.

i2 * 12 is of the type int. int to byte assignment is not allowed.

int i2 = 10; 
byte b2 = 5;   

b2 = i2 * 12; // An error. 

To fix the error, add casting.

b2 = (byte)(i2 * 12); // OK

i2 * b2 is of the type int and int to float assignment is allowed

int i2 = 10; 
byte b2 = 5;  
float f2 = 2.5F; 
f2 = i2 * b2; // Ok. 

d2 * i2 is of type double and double to float assignment is not allowed

float f2 = 2.5F; 
double d2 = 15.45;   
int i2 = 10; 
f2 = d2 * i2; // Error.

To fix the error, add casting

      
f2 = (float)(d2 * i2); // Ok