程序如下:
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是什么东西?
两次数组打印为何效果不一样 ?谢谢!
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的hashcode
[C@a90653似乎是hashcode的结果
但
System.out.println("the array is " + ch );
System.out.println( c );
调用方式相同阿
那为何两次调用方式相同 结果不一样呢?
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().
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 );,会看到和第一个句是一样的结果;我也是小菜鸟,不知道是不是解释的清楚~~~
kinzz() 所言极是
大家发帖很快~~~
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这种问题
例如类C有两个属性String str和int i,c是类C的一个对象,现在你要用System.out.println把c打印出来,但是c有两个属性,系统怎么样才把两个属性都打印出来呢?--呵呵,系统没有办法^_^ 只好打印出一个地址来了。