日期:2014-05-20  浏览次数:20752 次

问一个很水的问题,不要笑,请认真回答 呵呵 先谢谢了
程序如下:
import   java.io.*;
public   class   fd   {  
public   static   void   main(String[]   args)   throws   IOException   {
//   TODO   Auto-generated   method   stub
int   I   =   0;
char[]   ch   =   new   char[5];
while   (I++   <   1)   {  
InputStreamReader   stdin   =   new   InputStreamReader(System.in);
System.out.print( "Enter   a   line: ");

System.out.println(stdin.read(   ch   )   );
System.out.println( "the   array   is   "   +   ch   );
System.out.println( "the   array   is   "   +   ch[0]   );
}  
for(   int   i=0;   i <ch.length;   i++   )
System.out.println(   ch[i]   );

char[]   c   = "gfdsgs ".toCharArray();
System.out.println(   c   );
}
}


运行结果如下:
Enter   a   line:fd
4
the   array   is   [C@a90653
the   array   is   f
f
d


有的字符这里显示不了,   最好各位能亲自运行下,  
想问的问题是:
[C@a90653是什么东西?
两次数组打印为何效果不一样   ?

谢谢!

------解决方案--------------------
ch.toString()方法的自动调用
ch的hashcode
------解决方案--------------------
Object.toString()

This is a handy method. You can call it explicitly, and it will return a string that "textually represents " this object. It 's actually the name of the class of the object, the "@ " sign, and the address of the object. Use it while debugging to see if your objects really are what you thought they were. These lines in file ClockView.java will invoke Object 's toString() on a Timestamp object, and print the String.


------解决方案--------------------
System.out.println( "the array is " + ch );
System.out.println( c );
are different:
System.out.println( "the array is " + ch ); is calling System.out.println(String str)
System.out.println( c ); is calling System.out.println( char[] charArray);

And "the array is " + ch will automaticlly call "the array is " + ch.toString().
------解决方案--------------------
To Lz
System.out.println( "the array is " + ch );
System.out.println( c );
的调用结果之所以不同在于 "the array is " + 这个东东;
用了+连接,ch就要在打印前转化为String类型,实际上是用的println(String)重载;
而第二句的c依然是Char[],使用println(char[])重载,你可以把System.out.println( c );改写成System.out.println( "asdfds " + c );,会看到和第一个句是一样的结果;

我也是小菜鸟,不知道是不是解释的清楚~~~

------解决方案--------------------
jungle1171(jungle) ( ) 信誉:100 Blog 2007-02-08 17:23:38 得分: 0


To Lz
System.out.println( "the array is " + ch );
System.out.println( c );
的调用结果之所以不同在于 "the array is " + 这个东东;
用了+连接,ch就要在打印前转化为String类型,实际上是用的println(String)重载;
而第二句的c依然是Char[],使用println(char[])重载,你可以把System.out.println( c );改写成System.out.println( "asdfds " + c );,会看到和第一个句是一样的结果;

我也是小菜鸟,不知道是不是解释的清楚~~~



他说到了问题所在,我已经试过了,的确是因为 "the array is " + 的问题,要是去掉就可以了,不会再出现the array is [C@a90653这种问题