why does a char + another char = a weird number

前端 未结 4 1725
猫巷女王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:04

    If you want to have a String as result of the + operator you have to use type String as operands.

    You should write:

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

    The + operator applied on char operands behaves as the arithmetic sum.

提交回复
热议问题