This question already has answers here:
What's the simplest way to print a Java array?
                                
                                    (33个答案)
                                
                        
                                2年前关闭。
            
                    
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


为什么黑白输出有所不同?请解释一下

最佳答案

情况1 :

使用char[]方法的overloaded println在内部处理了数组并打印了String。请注意,对于任何其他类型的基本数组,println()没有这样的重载方法。

情况二:

您将数组与String连接起来,因此,即使在通过println处理之前,步骤1本身仍会调用char数组toString()并将其附加到string上,并将最终输出打印为String。

因此,如果您扩展第二条语句,处理过程将像

System.out.println("Output="+c.toString());
System.out.println("Output="+ "[C@15db9742");
System.out.println("Output=[C@15db9742");

关于java - Java字符串数组输出差异,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45117320/

10-14 11:54
查看更多