Promotion in Java?

Promotion rules are "when the operands are of different types, automatic binary numerical advancement occurs with a smaller conversion of the operand type to a larger one." But operands are of the same type, for example,

byte=byte+byte // Compile time error... found int.. 

So why is this so?

+16
java
Nov 02 '09 at 11:55
source share
4 answers

There is no no + operator for byte . Instead, both operands advance to int, so you have

 byte = byte + byte ... becomes (widening to find + operator) ... byte = int + int ... becomes (result of + operator) ... byte = int 

... which then fails because there is no implicit conversion from int to byte . You need to quit:

 byte a = 1; byte b = 2; byte c = (byte) (a + b); 

Here are the actual rules for digital advertising, from JLS section 5.6.2 :




When an operator applies binary numeric promotion to a pair of operands, each of which must indicate a value that can be converted to a numeric type, the following rules apply in order to use the advanced conversion (Β§5.1.2) to convert the operands if necessary:

  • If any of the operands has a reference type, the decompression conversion is performed (Section 5.1.8). Then:
  • If one of the operands is of type double, the other is converted to double.
  • Otherwise, if either operand is of type float, the other is converted to float.
  • Otherwise, if either operand is of type long, the other is converted to long.
  • Otherwise, both operands are converted to type int.
+35
Nov 02 '09 at 11:57
source share
β€” -

You were given the correct answer about automatic advertising on "int".

There is another point: complex assignment operators behave as if they are of an implicit type. Example:

 byte b1 = 1; byte b2 = 2; b1 = b1 + b2; // compilation fails b1 += b2; // compilation successful 
+14
Nov 02 '09 at 12:18
source share

From this question SO and above, the answers associated with the arithmetic operator + , both operands are converted to type int .

 byte b1 = 1; byte b2 = 2; byte b3 = b1 + b2; // compile time error 

In the above code, the values ​​of b1 and b2 will be resolved at run time, so the compiler will convert both values ​​to int before resolving the value.

But if we look at the following code,

 final byte b1 = 1; final byte b2 = 2; int b3 = b1 + b2; // constant expression, value resolved at compile time 

b1 and b2 are final variables, and the values ​​will be resolved at compile time, so compilation will not end.

0
Jan 17 '14 at 13:25
source share

I would like to talk about progress in general

Java can evaluate only arithmetic expressions in which the types of operands are identical

For example, in an expression containing int and double values, int values ​​are raised to double values ​​for use in the expression.

in another word

 double someVar = 1 / 2;// someVar = 0 

but

 double someVar = (double)1 / 2;// someVar = 0.5 

why?

  • we use (double) cast operator to create temporary floating-point of its operand "1" (it is called explicit conversion )
  • The calculation now consists of a floating point value (temporary double copy 1) divided by an integer 2
  • according to the above statement, Java performs an operation called promotion (or implicit conversion), so int values raised to double values for use in expression => integer 2 is raised to double
  • expression has become double someVar = 1.0 / 2.0; // someVar= 0.5 double someVar = 1.0 / 2.0; // someVar= 0.5

hope this is helpful even if it is not at the center of the issue

0
Feb 07 '17 at 16:07
source share



All Articles