Why is printing an Integer array Object giving a hash code and a char array object giving a value in java?

Here is a snippet of code

public class Test1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        char[] a={'a','b','c',97,'a'};
        System.out.println(a);
        int[] a1={8,6,7};
        System.out.println(a1);
        Integer[] b={10,20,30};
        System.out.println(b);
        }
}

Here is the conclusion

abcaa
[I@239d5fe6
[Ljava.lang.Integer;@5527f4f9

I know that he must deal with the method toString(). It was overridden in char to return a value. therefore, we get the first result, as expected here - an overridden method toString() java.lang.Character..

public String toString() {
      char buf[] = {value};//The value of the Character.
      return String.valueOf(buf);
   }

But looking at Integer, there is also an overridden method toString()

public String toString() {
        return String.valueOf(value); //The value of the Integer.
     }

Then why print codes a1 and b calls the default implementation toString()of the Object class, namely:

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

Also, since valueOf creates another object, but then it is common to both overridden methods.

+4
source share
6 answers

char :

https://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#println(char[])

public void println(char[] x)

, . , print(char[]), println().

:
x - . ​​

:

public void println(char x[]) {
    synchronized (this) {
      print(x);
      newLine();
   }
}

toString char[].

+7

- java.io.PrintSream char[]

/**
 * Prints an array of characters and then terminate the line.  This method
 * behaves as though it invokes <code>{@link #print(char[])}</code> and
 * then <code>{@link #println()}</code>.
 *
 * @param x  an array of chars to print.
 */
public void println(char x[]) {
    synchronized (this) {
        print(x);
        newLine();
    }
}

private void write(char buf[]) {
        try {
            synchronized (this) {
                ensureOpen();
                textOut.write(buf);
                textOut.flushBuffer();
                charOut.flushBuffer();
                if (autoFlush) {
                    for (int i = 0; i < buf.length; i++)
                        if (buf[i] == '\n')
                            out.flush();
                }
            }
        }
        catch (InterruptedIOException x) {
            Thread.currentThread().interrupt();
        }
        catch (IOException x) {
            trouble = true;
        }
    }

char[]

println whith int[] Integer[], , , , Object

 public void println(Object x) {
        String s = String.valueOf(x);
        synchronized (this) {
            print(s);
            newLine();
        }
    }
+1

, :

 int[] a1={8,6,7};
 System.out.println(a1);

Integer[].toSTring(), Integer.toSTring()

, Integer[].toSTring(), toString() .

, :

 System.out.println(a1[0]);
 System.out.println(a1[1]);
 System.out.println(a1[2]);

.

0

printStream char [], int [] , , fo type char [], Object whee toString(), .

, api

System.out.println(Arrays.toString());

0

Because you are converting this object to a string. You should use the toString () method for java.util.Arrays like: "Arrays.toString (a1); Arrays.toString (b)".

0
source

Since you are not calling int toString or Integer toString, you are calling Integer [] toString. This creates a textual representation of the objects stored in the array, rather than a textual representation of the value of the individual objects that are stored.

-2
source

Source: https://habr.com/ru/post/1584171/


All Articles