简体   繁体   English

为什么已经存在“ println(Object x)”时为什么需要“ println(char [] x)”-Java

[英]why need of “println(char[] x)” when there is already “println(Object x)” - java

I was reading about println function and I came across that there is println(char[ ] x) as well as println(Object x) 我在阅读有关println函数的信息时,发现有println(char [] x)println(Object x)
https://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#println(char[]) https://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#println(char [])

My question is that: As arrays in java are object so what is the need to specifically overload println() with char[] whereas rest arrays like int[] etc. uses the println(Object x) overloaded function. 我的问题是:由于Java中的数组是对象,因此有必要用char [] 专门重载println(),而像int []等其他数组则需要使用println(Object x)重载函数。

 println(Object x)

if you use it to print a char array (the char array is an object), it won't print the content but the objectClass@hashcode style. 如果使用它来打印一个char数组(char数组是一个对象),它将不会打印内容,而是objectClass @ hashcode样式。 You can test it yourself to see the exact output. 您可以自己对其进行测试以查看确切的输出。

Because they are implemented differently. 因为它们的实现方式不同。

println(Object)

will (after checking for null , etc), call the parameter's toString() method and display the result of it. 将(在检查null等之后),调用参数的toString()方法并显示其结果。

The toString() method of an array is not useful: it will give you the array type and the hashcode of the array. 数组的toString()方法没有用:它将为您提供数组类型和数组的哈希码。 So the overloaded form gives a more useful implementation in the case of a char[] parameter. 因此,在使用char[]参数的情况下,重载形式提供了更有用的实现。

Note that, with most object types, the toString() method can be overridden (so overloading the println(...) method for every possible type is not necessary (or possible...). However, the toString() method cannot be overridden for arrays, so there is benefit to overloading println in this case. 请注意,对于大多数对象类型, toString()方法可以被覆盖(因此不必为每种可能的类型重载println(...)方法(或可能...)。但是, toString()方法不能被数组覆盖,因此在这种情况下重载println有好处。

Because it prints the char array as a string and otherwise prints in object form, and seeing the contents may be more convinient. 因为它将char数组打印为字符串,否则以对象形式打印,因此查看内容可能更方便。 You can try casting it to object first and see the difference. 您可以先尝试将其投射到对象上,然后看看有什么不同。

Because print/ln(char[]) handles the actual printing of characters, toString() of the array object itself still provides the usual type+hash output, regardless of being an array of characters 因为print/ln(char[])处理字符的实际打印,所以数组对象本身的toString()仍然提供通常的type + hash输出,而不管字符数组如何

char c[]={'a','b','c'};
Object o=c;
System.out.println(c);
System.out.println(c.toString());
System.out.println(o); // *
System.out.println(o.toString());

The * thing is interesting (and this is why I post at all, since the rest is already there in other answers) because it demonstrates that Java has single dispatch: the actual method to be invoked is decided in compilation time, based on the declared type of the argument(s). *事情很有趣(这就是为什么我要发布,因为其余内容已经在其他答案中发布了),因为它表明Java具有单一调度:要调用的实际方法是在编译时根据所声明的来确定的。参数的类型。 So it does not matter that o is a character array in runtime, in compilation time it seems to be an Object , and thus print/ln(Object) is going to be invoked for it. 因此,在运行时o是一个字符数组并不重要,在编译时它似乎是一个Object ,因此将为此调用print/ln(Object)

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

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