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?
System.out is PrintStream which has a special method for char[] arg
int[] is printed via this method
System.out
actually gives you an Object ofPrintStream
. and itsprintln(params)
has some overloaded methods which are implemented differently for different type ofparams
.This
params
if being achar[]
provides the output aschar[]
elements likeSystem.out.println(charArray);
outputsabc
but not the hashcode as in the case ofint[]
likeSystem.out.println(intArray);
outputs[I@19e0bfd
.PS :- All the
array
are treated asObject
in Java.See the Official Doc
Moreover, for printing
array
, always use theArrays
utility class. Its methodArrays.toString()
should be preferably used for printingarray
objects.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 theSystem.out
instance) has a special version ofprintln
for character arrays -public void println(char x[])
- so it doesn't have to calltoString
for that array. It eventually callspublic 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, thepublic void println(Object x)
overload is chosen, which callsString.valueOf(x)
, which callsx.toString()
, which returns something like[I@19e0bfd
for int arrays.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 havechar[]
overloads for a simple call.However, formatting does not have a special case:
each outputs
[C@hash
, similar to the handing ofint
and other type arrays.Nor does concatenation, often used in printing:
outputs
the value of charArray is [C@hash
.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:
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'.
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)
, injava.io.PrintStream
class that prints out all characters from achar array
. And that is the reason, when you pass achar[]
as a parameter toprintln()
, it doesn't call thetoString()
method of thejava.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[])