JAVA程序,初级..运行异常..懂的进..谢谢..

来源:百度知道 编辑:UC知道 时间:2024/05/28 05:19:39
public class Example2
{
public static void main(String [] args)
{
String s=new String("world");
char ch[]= {'H','e','l','l','o'};

System.out.println(s+" and "+ch);
}

}

运行结果本来是 Word and Hello

编译通过了..可是运行为什么是 Word and [C@de6ced
不明白了..望高手指点..谢谢...

ch是个数组,也就是一个Object对象你把它直接输出就是调用改对象的toString()方法,也就是父类Object的toString()
public String toString()返回该对象的字符串表示。通常,toString 方法会返回一个“以文本方式表示”此对象的字符串。结果应是一个简明但易于读懂。建议所有子类都重写此方法。
Object 类的 toString 方法返回一个字符串,该字符串由类名(对象是该类的一个实例)、at 标记符“@”和此对象哈希码的无符号十六进制表示组成。换句话说,该方法返回一个字符串,它的值等于:
getClass().getName() + '@' + Integer.toHexString(hashCode())

详细见JDk的api,要达到你想要的应该改成
System.out.println(s+" and "+new String(ch));

你可以这样输出:
System.out.println(s+" and "+new String(ch));
或者
System.out.println(s+" and "+String.valueOf(ch));

String s=new String("world");
char ch[]= {'H','e','l','l','o'};
/*ch与字符串相加时调用 toString 方法返回一个字符串,
* 该字符串由类名(对象是该类的一个实例)、at 标记符“@”
* 和此对象哈希码的无符号十六进制表示组成。
*
* */
System.out.println(s+" and "+ch);
//用new String避免ch执行toString 方法
System.out.println(s+" and &q