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

后端 未结 6 1786
鱼传尺愫
鱼传尺愫 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:19

    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();
        }
    }
    

提交回复
热议问题