Why is only the println method overloaded for an array of characters, but not for other arrays like String, Integer, etc.?

Why the println method has another overloaded method only for an array of characters, but not for other arrays like String, Integer, etc.

for instance

int intArray[] = {0,1,2};
char charArray[] = {'a','b','c'};
System.out.println(intArray);
System.out.println(charArray);

Conclusion:

[I@19e0bfd
abc
+4
source share
3 answers

Most likely because it was created around for writing to character streams .

System.outis PrintStreamwhich delegates the entry to BufferedWriter, which, in turn, is an instance Writer.

some Writerfeatures

void    write(char[] cbuf)
abstract void   write(char[] cbuf, int off, int len) 
void    write(int c)
void    write(String str) 
void    write(String str, int off, int len)

Because of this, in most cases, each void print(..)method in PrintStreamuses String.valueOf()to pass it on Writerand saywriter.write(s)

toString https://bugs.openjdk.java.net/browse/JDK-4168079, , , , - /. , , .

, System.out.println(Arrays.toString(new int[]{1,2,3}))

+1
0

, , , , , char s. : , , toString(), Arrays. , , char. :

Arrays.toString(yourArray);

, :

for (YourClass object : yourArray) {
    System.out.println(object);
} 
0

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


All Articles