4

Why println method has a different overloaded method only for character array but not for other arrays such as String,Integer etc?

For example

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

Output:

[I@19e0bfd
abc
Prasad
  • 1,089
  • 13
  • 21
  • 2
    most likely because those who implemented it chose it that way. In the end, were they supposed to create an overloaded method for each type that would ever come into existence? – Stultuske Jan 30 '18 at 12:30
  • Because a String represents an array of characters (and not an array of any other type)? – Bernhard Barker Jan 30 '18 at 12:45
  • I am expecting answer from you all? – Prasad Jan 30 '18 at 12:48
  • Does this answer your question? [If a char array is an Object in Java, why does printing it not display its hash code?](https://stackoverflow.com/q/31218498) (EDIT: oh, you saw that already) – Bernhard Barker Jan 30 '18 at 13:00
  • I know the answer for the above question as there are overloaded methods of println in PrintStream class. My question is not the same. Why they didn't overload this method for other primitive types arrays or is there any reason behind overloading the character array method. – Prasad Jan 30 '18 at 13:45

3 Answers3

1

Most likely because it was designed all around writing to character streams.

System.out is a PrintStream which delegate writes to a BufferedWriter which in turn is instance of Writer.

some Writer possibilities

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 that mostly every void print(..) method in PrintStream uses String.valueOf() to be able to pass it over to writer and say writer.write(s)

This was noticed and proposed to implement toString in arrays https://bugs.openjdk.java.net/browse/JDK-4168079, but obviously it was too late, due to compatibility/stability concerns. So the decision was to Implement helper methods to accomplish the same thing.

So now you may find a lot of System.out.println(Arrays.toString(new int[]{1,2,3}))

yvoytovych
  • 871
  • 4
  • 12
0

System.out is a PrintStream (link) so it has several methods like :

Because you cannot overload a method for each type of object in Java, the choice has been to do only this one

azro
  • 53,056
  • 7
  • 34
  • 70
0

Seems like designers decided it because in their opinion it would be the most used way of printing array containing chars. Note that if you want to print contents of an array instead of printing its address in memory, you can use method toString() provided in Arrays class. It lets you print contents of an array in a convenient way when array contains other types of objects than char. You call it this way:

Arrays.toString(yourArray);

Another way to print contents of array is creating a loop iterating over elements of array:

for (YourClass object : yourArray) {
    System.out.println(object);
} 
Przemysław Moskal
  • 3,551
  • 2
  • 12
  • 21