java - Multiplying two bytes -


can explain me why can't multiply 2 bytes in way?

byte = 1; byte b = 1; byte c = a*b; 

or

byte = 1; byte b = 1; short c = a*b; 

why have in way?

byte = 1; byte b = 1; byte c = (byte)(a*b); 

or

byte = 1; byte b = 1; int/double/float/long c = a*b; 

when performing math bytes, binary numeric promotion takes place, specified jls, section 5.6.2.

when operator applies binary numeric promotion pair of operands, each of must denote value convertible numeric type, following rules apply, in order:

  1. if operand of reference type, subjected unboxing conversion (§5.1.8).

  2. widening primitive conversion (§5.1.2) applied convert either or both operands specified following rules:

    • if either operand of type double, other converted double.

    • otherwise, if either operand of type float, other converted float.

    • otherwise, if either operand of type long, other converted long.

    • otherwise, both operands converted type int.

(emphasis mine)

that forces assign type @ least wide int or cast byte.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -