字符数组的默认值

时间:2015-08-12 05:12:59

标签: java arrays

字符数组的默认值是给出一个空指针异常...可以告诉为什么这个异常仅适用于字符数组...即使其他dataype数组的默认值为null。 例如

class  Test{  
    char[] x;
    public static void main(String[] args) {
        Test t=new Test();
        System.out.println(t.x);    
    }
}

抛出空指针异常

class  Test{
    int[] x;
    public static void main(String[] args) {
        Test t=new Test();
        System.out.println(t.x);
    }
}

输出:空

3 个答案:

答案 0 :(得分:28)

打印char[]的行为与其他阵列不同,因为PrintStreamSystem.out实例的类型)具有打印char数组的特定方法 - { {1}} - 对于其他数组,使用public void println(char x[])的常规方法 - Object

public void println(Object x)打印字符串" null"向其传递println(Object x)引用时。

null

/** * Prints an Object and then terminate the line. This method calls * at first String.valueOf(x) to get the printed object's string value, * then behaves as * though it invokes <code>{@link #print(String)}</code> and then * <code>{@link #println()}</code>. * * @param x The <code>Object</code> to be printed. */ public void println(Object x) { String s = String.valueOf(x); synchronized (this) { print(s); newLine(); } } /** * Returns the string representation of the <code>Object</code> argument. * * @param obj an <code>Object</code>. * @return if the argument is <code>null</code>, then a string equal to * <code>"null"</code>; otherwise, the value of * <code>obj.toString()</code> is returned. * @see java.lang.Object#toString() */ public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); } 在传递println(char x[])引用时会抛出NullPointerException

null调用public void println(char x[]),调用public void print(char s[]),在评估public void write(char buf[])时会引发NullPointerException

buf.length

顺便说一下,/* * The following private methods on the text- and character-output streams * always flush the stream buffers, so that writes to the underlying byte * stream occur as promptly as with the original PrintStream. */ 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; } } 的Javadoc是print(char s[])调用的第一个方法,它提到public void println(char x[])例外:

  

void java.io.PrintStream.print(char [] s)

     

打印一个字符数组。根据平台的默认字符编码将字符转换为字节,这些字节的写入方式与write(int)方法完全相同。

     

参数:
  s要打印的字符数组
  的抛出:
  NullPointerException - 如果s为null

答案 1 :(得分:4)

第一个使用"...<img src='/can/be/.../anything/1.jpg'> ... <img src='/unknown/.../path/xx.jpg'>.." ,第二个使用println(char[] x),内部使用println(Object obj)并打印String.valueOf(x)代替null

检查NullPointerException

String.valueOf()

如果 public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); } println(char[] x),根据java doc NullPointerException将会抛出char[] arr

答案 2 :(得分:0)

当应用程序在需要对象的情况下尝试使用null时,抛出NullPointerException。其中包括:

  1. 调用null对象的实例方法。
  2. 访问或修改空对象的字段。
  3. 将null的长度视为数组。
  4. 访问或修改null的插槽,就像它是一个数组一样。
  5. 抛出null,就好像它是一个Throwable值。
  6. 要了解有关NullPointerException的更多信息,请参阅Oracle文档。以上来源取自NullPointerException