6

case 1:

char c='A'+'A'; 
System.out.println(c);   // print question mark (?)

case 2:

char c1='A';
char c2='A'+c1;   // Compile time error
System.out.println(c2);   

In case1 I have logic When I am adding two char literals, the sum of ascii value is printing which is equivalent to ascii value of question mark. But in case2 why compile time error is showing that 'cannot convert from int to char'

if char+char=int then this rule must be apply for both cases.

Randhir
  • 324
  • 3
  • 10
  • This question applies to all integer types where a narrowing conversion at runtime would be required. (And, that's the only way that this makes sense because what's the meaning of adding a UTF-16 code unit to a UTF-16 code unit?) – Tom Blodget Jun 12 '18 at 16:57

4 Answers4

3

In the first case two final chars are added and at compile time it is checked that the resulting int does not overflow a char.

In the second case one operand is not final, and the compiler refuses to do a sum. A bit dumb, but considering multithreading and other cases, maybe justifiable. At least an easily understood rule.

Doing

final char c1 = '0';
char c2 = '1' + c1; // 'a'

should compile.

(It goes without say, that one should never do something like '0'+'1'.)

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • I suppose it would be possible to extend the definition of ["constant variable"](https://docs.oracle.com/javase/specs/jls/se10/html/jls-4.html#jls-4.12.4) to include "effectively final" variables initialized with ["constant expression"](https://docs.oracle.com/javase/specs/jls/se10/html/jls-15.html#jls-15.28) in addition to final ones, but as always there is a cost/benefit-tradeoff to consider – Hulk Jun 12 '18 at 14:14
  • On second thought, such a change in the JLS would not be backwards-compatible because it would have implications on which code gets evaluated at runtime vs. compile time, which code is considered unreachable and - probably most difficult to fully anticipate all consequences of - the [class initialization](https://docs.oracle.com/javase/specs/jls/se10/html/jls-12.html#jls-12.4.2) – Hulk Jun 12 '18 at 14:30
1

Addition or 2 or more variables which are of type int or lower than int results in the type int.

So what you are doing with char c2 = 'A' + c1; is actually not giving you a char result. It is giving you int result which you are trying to implicitly type cast to byte, which gives you the compile time error.

Summary Char + Char = Int And Char cannot directly store Int.

Try and do this instead char c2 = (char)('A' + c1);

Here you explicitly type cast the int value to char type Telling the compiler that you are ready to accept the Lossy Conversion that may happen

1

In the second case there is an error because, by JLS 5.6.2 both operands of the binary expression are converted to int.

To make it work you should add explicit cast:

char c2=(char)('A'+c1);
NiVeR
  • 9,644
  • 4
  • 30
  • 35
  • both operands of the binary expression are converted to int. is also applicable for 1st case. but it working fine. – Randhir Jun 12 '18 at 13:16
0

The result of adding Java chars, shorts, or bytes is an int

See the detailed answers on this post: In Java, is the result of the addition of two chars an int or a char?

Bits Please
  • 318
  • 1
  • 11