String s="StackOverflow is heaven";
char []c=new Char[100];
c=s.toCharArray();
System.out.println(c);
System.out.println("Output="+c);
Output is :- StackOverflow is heaven
Output=[C@15db9742
Run Code Online (Sandbox Code Playgroud)
为什么有不同的黑白输出?请解释一下
情况1 :
该重载的println这需要char[]方法处理您的阵列内部和打印的字符串。请注意,println()对于任何其他类型的原始数组,没有此类重载方法。
案例2:
您正在将数组与 String 连接起来,因此在通过 println 处理之前,步骤 1 本身toString()会调用您的 char 数组并将其附加到字符串,并将最终输出打印为字符串。
因此,如果您展开第二个语句,处理过程如下所示
System.out.println("Output="+c.toString());
System.out.println("Output="+ "[C@15db9742");
System.out.println("Output=[C@15db9742");
Run Code Online (Sandbox Code Playgroud)