Adding char and int

前端 未结 7 2076
梦如初夏
梦如初夏 2020-11-30 10:51

To my understanding a char is a single character, that is a letter, a digit, a punctuation mark, a tab, a space or something similar. And therefore when I do:

7条回答
  •  不知归路
    2020-11-30 11:19

    You end up with out of 50 because you have told Java to treat the result of the addition as an int in the following line:

    int ans = a + c;
    

    Instead of int you declare ans as a char.

    Like so:

    final int a = 1;
    final char c = '1';
    final char ans = (char) (a + c);
    System.out.println(ans);
    

提交回复
热议问题