java中关于char[]数组输出问题

今日在论坛上看到一个帖子发现了一个问题,以前这个问题没有注意到,今日特意记录下来,进行分享,希望能够为java学习带来一些帮助。

public class First {

	public static void main(String[] args) {
		
		char[] ch = {'a','b','c'};
		System.out.println(ch);
		System.out.println("ch"+ch);
	}
}

输出结果为:

abc
ch[C@15db9742

这样的结果存在疑问,为什么第二次的输出结果不是chabc


分析:

System.out.println(ch);//调用的println方法的参数是char[]数组,println方法的源代码:

 /**
     * Prints an array of characters and then terminate the line.  This method
     * behaves as though it invokes <code>{@link #print(char[])}</code> and
     * then <code>{@link #println()}</code>.
     *
     * @param x  an array of chars to print.
     */
    public void println(char x[]) {
        synchronized (this) {
            print(x);
            newLine();
        }
    }


System.out.println("ch"+ch);//调用的println方法的参数是char[]数组,println方法的源代码:

/**
     * Prints a String and then terminate the line.  This method behaves as
     * though it invokes <code>{@link #print(String)}</code> and then
     * <code>{@link #println()}</code>.
     *
     * @param x  The <code>String</code> to be printed.
     */
    public void println(String x) {
        synchronized (this) {
            print(x);
            newLine();
        }
    }


这两次调用的println方法是不同的,
第一次调用的方法的参数是char[]数组类型
第二次调用的方法的参数是String类型的,因为“ch”+ch把数组转化成了字符串String类型的了
这是println方法的重载问题导致的。


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值