可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Printing a char array does not display a hash code:
class IntChararrayTest{
public static void main(String[] args){
int intArray[] = {0,1,2};
char charArray[] = {'a','b','c'};
System.out.println(intArray);
System.out.println(charArray);
}
}
output :
[I@19e0bfd
abc
Why is the integer array printed as a hashcode and not the char array?
回答1:
First of all, a char array is an Object in Java just like any other type of array. It is just printed differently.
PrintStream
(which is the type of the System.out
instance) has a special version of println
for character arrays - public void println(char x[])
- so it doesn't have to call toString
for that array. It eventually calls public void write(char cbuf[], int off, int len)
, which writes the characters of the array to the output stream.
That's why calling println for a char[]
behaves differently than calling it for other types of arrays. For other array types, the public void println(Object x)
overload is chosen, which calls String.valueOf(x)
, which calls x.toString()
, which returns something like [I@19e0bfd
for int arrays.
回答2:
The int array is an array of integers where as the char array of printable characters. The printwriter has the capability to print character arrays as this is how it prints string anyway. The printwriter will therefore print them like a string, without calling the toString() method to convert it to a string. Converting an int array to a string returns a hash code, explaining why you get that output.
Take this for example:
int[] ints = new int[] { 1, 2, 3 };
char[] chars = new char[] { '1', '2', '3' }
If you were to print both those sequences using the method you used, it would print the hash code of the int array followed by '123'.
回答3:
System.out is PrintStream which has a special method for char[] arg
public void println(char x[]) {
synchronized (this) {
print(x);
newLine();
}
}
int[] is printed via this method
public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}
回答4:
Ofcourse arrays are objects in Java. No matter what type of array it is. It is always an object in Java.
But as far as your question is concerned, there is a method, println(char[] array)
, in java.io.PrintStream
class that prints out all characters from a char array
. And that is the reason, when you pass a char[]
as a parameter to println()
, it doesn't call the toString()
method of the java.util.Array
class, but rather loops through the array and prints every character.
Citation:
http://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#println(char[])
回答5:
System.out
actually gives you an Object of PrintStream
. and its println(params)
has some overloaded methods which are implemented differently for different type of params
.
This params
if being a char[]
provides the output as char[]
elements like System.out.println(charArray);
outputs abc
but not the hashcode as in the case of int[]
like System.out.println(intArray);
outputs [I@19e0bfd
.
PS :- All the array
are treated as Object
in Java.
See the Official Doc
Moreover, for printing array
, always use the Arrays
utility class. Its method Arrays.toString()
should be preferably used for printing array
objects.
回答6:
The other answers correctly explain your case of a separate call to PrintStream.println
and the same is true for .print
. However your title could cover other ways of printing. PrintWriter.println
and .print
AND .write
do have char[]
overloads for a simple call.
However, formatting does not have a special case:
System.out.format ("%s%n", charArray); // or printf
myPrintWriter.format ("%s%n", charArray); // or printf
each outputs [C@hash
, similar to the handing of int
and other type arrays.
Nor does concatenation, often used in printing:
System.out.println ("the value of charArray is " + charArray);
outputs the value of charArray is [C@hash
.