In Java, the following is allowed:
char c = 'A' + 1;
Here, c will hold the value 'B'. Above, first the expression is evaluated. So 'A' gets converted to 65, the whole expression evaluates to 66, and then 66 is converted to 'B' since we are storing the value in a char.
The following, however, gives a compile-time error:
char c = 'A';
c = c + 1;
What is the explanation for how Java views the expressions differently? By the way, the following works fine too:
char c = 'A';
c++;
It is because the compiler can check that it
('A' + 1)
is within the bounds of a char whereas it cannot (in general) check thatc + <an integer>
is within the bounds.char to int conversion is called widening conversions. In widening conversions, values do not lose information about the overall magnitude of a numeric value where as int to char conversion is called narrowing conversions. With narrowing conversion you may lose information about the overall magnitude of a numeric value and may also lose precision.
For more information on primitive conversions refer this document.
The first example (which compiles) is special because the addition happens in the variable's initializer, and both operands are literals.
A few definitions to start with:
Converting an
int
tochar
is called a narrowing primitive conversion, becausechar
is a smaller type thanint
.'A' + 1
is a constant expression. A constant expression is (basically) an expression whose result is always the same and can be determined at compile-time. In particular,'A' + 1
is a constant expression because the operands of+
are both literals.A narrowing conversion is allowed during the assignments of
byte
,short
andchar
, if the right-hand side of the assignment is a constant expression:c + 1
is not a constant expression, becausec
is a non-final
variable, so a compile-time error occurs for the assignment. From looking at the code, we can determine that the result is always the same, but the compiler isn't allowed to do that in this case.One interesting thing we can do is this:
In that case
a + 1
is a constant expression, becausea
is afinal
variable which is initialized with a constant expression.The caveat "if […] the value […] is representable in the type of the variable" means that the following would not compile:
The value of
'A' + 99999
(which is100064
, or0x186E0
) is too big to fit in to achar
, becausechar
is an unsigned 16-bit integer.As for the postfix
++
operator:(* Binary numeric promotion takes
byte
,short
andchar
operands of operators such as+
and converts them toint
or some other larger type. Java doesn't do arithmetic on integral types smaller thanint
.)In other words, the statement
c++;
is mostly equivalent to:(The difference is that the result of the expression
c++
, if we assigned it to something, is the value ofc
before the increment.)The other increments and decrements have very similar specifications.
Compound assignment operators such as
+=
automatically perform narrowing conversion as well, so expressions such asc += 1
(or evenc += 3.14
) are also allowed.Its because the literals for integer or smaller than int as byte ,short and char is int. Understand the following in this way.
code:
the same happens for any mathematical operations as of 'Divide', 'multiply', and other arithmetic operation. so cast the result to get the literal in desired data type
so when you perform
Its the result of c+1 is int not a char . so compiler give a compile time error for the same. so you need to provide a primitive cast to change the literal to char data type. Hope this example provide some understanding..