Java: println with char array gives gibberish

前端 未结 5 1974
悲哀的现实
悲哀的现实 2020-12-10 01:04

Here\'s the problem. This code:

String a = \"0000\";
 System.out.println(a);
char[] b = a.toCharArray();
 System.out.println(b);

returns

5条回答
  •  一整个雨季
    2020-12-10 01:29

    An array's toString() method (which is what's called when you do "..." + b) is only meant to give debugging output. There isn't a special case where a char[]'s toString() will give you the original string - arrays of all types have the same toString() implementation.

    If you want to get the original string from the char array, use:

    String a2 = new String(b);
    

提交回复
热议问题