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

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

    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.

提交回复
热议问题