why does a char + another char = a weird number

前端 未结 4 1715
猫巷女王i
猫巷女王i 2020-12-20 00:25

Here\'s the code snippet:

public static void main (String[]arg) 
{
    char ca = \'a\' ; 
    char cb = \'b\' ; 
    System.out.println (ca + cb) ; 
}
         


        
4条回答
  •  北海茫月
    2020-12-20 01:16

    The + operator doesn't operate over characters like it does over strings. What's happening here is that a and b are being cast to their integer ASCII codepoints - 97 and 98 - and then added together.

提交回复
热议问题