public static void main(String[] args) {

char[] ch=null;
         System.out.println(ch);
                  System.out .println("ch: "+ch);
    }
为什么System.out .println("ch: "+ch);会输出ch: null
而System.out.println(ch);会报出空指针异常,其它类型的数组都不会发生这种情况,只有char[]会这样。

解决方案 »

  1.   

    我认为:char[]这个数组指向null,就是个空指针,所以System.out.println(ch);很正常,而"ch: "+ch 转化为了String 就向 ""+ch ,ch 转化为String后就是null,所以不异常
      

  2.   

    java .io.Writer  public void write(char cbuf[]) throws IOException {
    write(cbuf, 0, cbuf.length);
        }
       cbuf.length 能不异常吗?
      

  3.   

    最好直接分析源代码,println(char[])最终调用下面的代码,有个for循环,它把buf看作不为null来处理而""+buf是自动调用Object.toString方法,如果是null的,就返回"null"
    所以一个有异常,一个没异常    private void write(char buf[]) {
    try {
        synchronized (this) {
    ensureOpen();
    textOut.write(buf);
    textOut.flushBuffer();
    charOut.flushBuffer();
    if (autoFlush) {
        for (int i = 0; i < buf.length; i++)
    if (buf[i] == '\n')
        out.flush();
    }
        }
    }
    catch (InterruptedIOException x) {
        Thread.currentThread().interrupt();
    }
    catch (IOException x) {
        trouble = true;
    }
        }
      

  4.   

    通俗的理解就是因为加了“ch”,system.out.println认为输出的是string啊