遇到个怪问题?

[复制链接]
查看11 | 回复5 | 2009-1-4 14:52:28 | 显示全部楼层 |阅读模式
char[] c = {'a', 'b', 'c'};
String[] s = {"a", "b", "c"};
System.out.println(c); //abc
System.out.println(s);//@lsdjfj
为什么
输出c是char[]中的东西,而String[]却是hashCode码 ?
回复

使用道具 举报

千问 | 2009-1-4 14:52:28 | 显示全部楼层
我做了测试

int[] i = {1, 2, 3};

short[] s = {2, 3, 4};

long[] l = {222, 333, 444};

char[] c = {'a', 'b', 'c'};

byte[] b = {1, 2, 3};

float[] f = {1.1f, 1.2f, 2.0f};

double[] d = {2.2, 3.3, 4.0};

boolean[] boo = {true, false, true};

System.out.println(i);

System.out.println(s);

System.out.println(l);

System.out.println(c);

System.out.println(f);

System.out.println(d);

System.out.println(b);

System.out.println(boo);
但只是char[]中的东西出来了,应和包裹类没关系。
请问为什么会这样?
回复

使用道具 举报

千问 | 2009-1-4 14:52:28 | 显示全部楼层
1 System.out.println 使用的是java.io.PrintStream的print方法输出
2 对于char[] PrintStream里边有print(char [])方法
print
public void print(char[] s)
Print an array of characters. The characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.
Parameters:
s - The array of chars to be printed
Throws:
NullPointerException - If s is null
3 PrintStream print(char[])的处理就是将数组中内容都打印出来
4 对于其他的数组,将其处理为ReflectArray ---- java.lang.reflect.Array 使用的是print(Object obj)
print
public void print(Object obj)
Print an object. The string produced by the String.valueOf(Object) method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.
Parameters:
obj - The Object to be printed
See Also:
Object.toString()
5 因为java.lang.reflect.Array并没有覆盖toString方法,所以打印的就是对象引用地址
回复

使用道具 举报

千问 | 2009-1-4 14:52:28 | 显示全部楼层
看看api就知道了, 不要在那瞎猜
回复

使用道具 举报

千问 | 2009-1-4 14:52:28 | 显示全部楼层
常用的,基本的一些类还是有必要熟悉一下
回复

使用道具 举报

千问 | 2009-1-4 14:52:28 | 显示全部楼层
这不是怪问题,呵呵
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行