Guys I recently discovered that char array can also be printed in java without for loop and the output is a string.But when i tried the same for int array, I got some gibberish value which i though to be memory address pointed by array as it contains reference to the array elements. I read the explanation given by one of the the programmer here but somehow I did not actually understand it.. Could anyone please explain it in a better way ??

Recommended Answers

All 5 Replies

I'll take a shot. I'm not as familiar as some other people, so please excuse any inarticulate phrasing regarding "reference" or "address", but hopefully it's clear enough.

No for-loop is needed to print the array elements. Java providestoString() methods in the Arrays class. There could be a for-loop "under the hood" there. See code below. As far as why it prints jibberish when you don't use the Arrays.toString() method, that is because it does not assume that it knows what you want so it prints a reference or a memory location or something like that. It's not exactly "jibberish", but it definitely does not display the contents of the array. As for why it prints 'abcd' when you print charArray below, I'm not positive but I believe that the Java implementors just decided to implement that as the default behavior because a character as opposed to anything else is the only array where it makes sense that that's what you want. If you look at the PrintStream class, it has a println(char[]) function. No other array type has that. System.out.println uses the PrintStream class somehow, I believe. I go warnings when compiling to make me aware of that. Tried to copy and paste the exact warning here, but for some reason could not on NetBeans. So basically, I think the underlying assumption in Java, as elsewhere (i.e. C) is that, unless specified otherwise, displaying an array of characters means displaying them as a string. Using the toString() method means display the reference/address of the array. Arrays.toString() means loop through the array and display each element, separated by a comma.

You didn't link the other post you read, so I cannot comment.

    int intArray[] = new int[]{1,2,3,4};
    char charArray[] = new char[]{'a','b','c','d'};
    System.out.println(intArray); // gibberish
    System.out.println(charArray); // abcd
    System.out.println(intArray.toString()); // gibberish
    System.out.println(charArray.toString()); // gibberish
    System.out.println(Arrays.toString(intArray)); // [1, 2, 3, 4]
    System.out.println(Arrays.toString(charArray)); // [a, b, c, d]

@Assert_null : So you trying to say that this unique behaviour of char array in java can be justified by the assumption that in JAVA displaying an array of characters means displaying them as a string.

No, it's not any kind of Java language assumption.
println is a method in the PrintStream class. (System.out is an instance of PrintStream). It's overloaded for all kinds of parameters, one of which is char[], and that version prints the array as a single String.
So the behaviour is an arbitrary choice made by the authors of the PrintStream class, that's all.

( All other arrays are handled by the version that takes Object as its parameter, and that version prints the object's toString result, whose default behaviour, inherited from Object, is to return a slightly coded version of the object's type followed by its hash.)

https://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html

@jamesCherrill : Thank you for your clear cut explanation. My doubt got cleared. I searched everywhere on internet but could not get satisfactory answer.. I got it here..From you! Thanks a ton!!! Cheers !

My pleasure.
Until next time.....
J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.