简体   繁体   English

char和int数组的区别

[英]char and int array difference

When I try to print the uninitialized static char array it gives run time error (Null pointer exception) whereas the uninitialized static int array gives null value. 当我尝试打印未初始化的静态字符数组时,它会给出运行时错误(空指针异常),而未初始化的静态int数组则给出空值。 Why? 为什么?

public class abc {
    static int arr[];
    static char ch[];

    public static void main(String[] args) {
        System.out.println(ch); //it gives null pointer exception at run time
        System.out.println(arr); //it gives output as "null".
        }
    }

The answer exists in the PrintWriter source code (of which System.out is an instance). 答案存在于PrintWriter源代码中(其中System.out是一个实例)。

Start with the fact that the uninitialized arrays, as reference variables, are given the default of null . 首先,未初始化的数组作为引用变量,默认为null

The println(char[]) (eventually) attempts to call .length on the passed in array. println(char[]) (最终)尝试在传入的数组中调用.length It's null, resulting in the NullPointerException . 它为null,导致NullPointerException println(char[]) (eventually) calls write(char[]) : println(char[]) (最终)调用write(char[])

public void write(char buf[]) {
    write(buf, 0, buf.length);
}

There is no overload of println matching int[] , but there is a println(Object) . println int[]匹配int[]没有重载,但是有一个println(Object) There it (eventually) attempts String.valueOf , passing the null reference, so String.valueOf takes the null and returns the String "null" . 在那里(最终)尝试String.valueOf ,传递null引用,因此String.valueOf获取null并返回String "null" println(Object) calls print(Object) : println(Object)调用print(Object)

public void print(Object obj) {
    write(String.valueOf(obj));
}

System.out is an instance of PrintStream and this class has a few overloaded println methods. System.outPrintStream一个实例,这个类有一些重载的println方法。 In your case: 在你的情况下:

  1. System.out.println(ch); is using public void println(char x[]) 正在使用public void println(char x[])
  2. System.out.println(arr); is using public void println(Object x) (there is no public void println(int[] x) method so the closest available type for int[] which println can use is Object ). 正在使用public void println(Object x) (没有public void println(int[] x)方法,因此println可以使用的int[]最接近的可用类型是Object )。

The second method is using 第二种方法是使用

String.valueOf(x);

to get a string representation of the object we want to print and the code of the valueOf method looks like 获取我们要打印的对象的字符串表示形式,以及valueOf方法的代码

public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
}

so it is null-safe (will return the string "null" if the reference holds null ). 所以它是null安全的(如果引用保持为null将返回字符串"null" )。

The first method is at some level using 第一种方法是在某种程度上使用

public void write(char cbuf[]) throws IOException {
    write(cbuf, 0, cbuf.length);
                 //    ^^^^^^^ this throws NPE
}

and because cbuf is null cbuf.length will throw NullPointerException because null doesn't have length (or any other) field. 并且因为cbufnull cbuf.length将抛出NullPointerException,因为null没有length (或任何其他)字段。

These are two different methods, and their APIs describe the behavior fully. 这是两种不同的方法,它们的API完全描述了这种行为。

public void println(Object x) calls String.valueOf at first, which returns "null" if x is null. public void println(Object x)首先调用String.valueOf,如果x为null,则返回“null”。

See: 看到:

public void print(char[] c) calls the print(char[] c) method which throws NullPointerException if c is null. public void print(char[] c)调用print(char [] c)方法,如果c为null,则抛出NullPointerException。

See: 看到:

You are actually calling two separate, overloaded System.out.println() methods. 您实际上正在调用两个单独的,重载的System.out.println()方法。 public void println(char[] x) is overloaded as per the documentation. public void println(char[] x)按照文档重载。 No specific overloading of println(Object x) exists for the int[] arrays. int[]数组不存在println(Object x)特定重载。

So, when println() is called on an integer array, public void println(Object x) is called. 因此,当在整数数组上调用println()将调用public void println(Object x) According to the Javadocs: 根据Javadocs:

This method calls at first String.valueOf(x) to get the printed object's string value, then behaves as though it invokes print(String) and then println(). 此方法首先调用String.valueOf(x)来获取打印对象的字符串值,然后表现为调用print(String)然后调用println()。

Since the value of the string is null , the method prints null. 由于字符串的值为null ,因此该方法打印为null。

The println() method works somewhat differently when it takes a character array as a parameter. 当将字符数组作为参数时, println()方法的工作方式有所不同。 Superficially, the method itself seems similar: 从表面上看,该方法本身似乎相似:

Prints an array of characters and then terminate the line. 打印一个字符数组,然后终止该行。 This method behaves as though it invokes print(char[]) and then println(). 此方法的行为就像调用print(char [])然后调用println()一样。

However, the print(char[]) method behaves quite differently in key ways: 但是, print(char[])方法在关键方面的表现完全不同:

Prints 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. 根据平台的默认字符编码将字符转换为字节,这些字节的写入方式与write(int)方法完全相同。

Thus, it calls a different method. 因此,它称之为不同的方法。 The documentation for print(char[]) explicitly states that in your situation, your exception is thrown: print(char[])的文档明确指出在您的情况下,抛出异常:

Throws: NullPointerException - If [input parameter] s is null 抛出:NullPointerException - 如果[input parameter] s为null

Hence, the cause of the difference in behavior. 因此,行为差异的原因。

See Javadocs for more information about the println and print methods: http://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#println() 有关printlnprint方法的更多信息,请参阅Javadocs: http//docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#println()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM