If a char array is an Object in Java, why does printing it not display its hash code?

后端 未结 6 1791
鱼传尺愫
鱼传尺愫 2020-12-02 01:38

Printing a char array does not display a hash code:

class IntChararrayTest{
    public static void main(String[] args){
        int intArray[] = {0,1,2};
            


        
6条回答
  •  庸人自扰
    2020-12-02 02:22

    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.

提交回复
热议问题