Why can I add characters to strings but not characters to characters?

前端 未结 7 769
陌清茗
陌清茗 2020-12-21 09:18

So I wanted to add a character to a string, and in some cases wanted to double that characters then add it to a string (i.e. add to it itself first). I tried this as shown b

7条回答
  •  有刺的猬
    2020-12-21 09:45

    String string = "" + X + X;

    is an example of concatenation. By pre-pending the empty string you specify that the result should be a string. The first line of code tells the compiler that you are adding 2 chars (or ints) which should result in an int, and not a string.

提交回复
热议问题