IO.println behavior on char[]
Chen Liang
chen.l.liang at oracle.com
Sun Jun 29 14:54:36 UTC 2025
Good observation -however, I do think disclosing that Java arrays are storage objects instead of functional objects like a list should be done as early as possible to Java learners. Doing so through the lack of toString override on arrays evident in IO.println is a good way.
(On a side note, the simplified main hides the argument array, which allows us to introduce array later into the learning process)
________________________________
From: amber-dev <amber-dev-retn at openjdk.org> on behalf of Ethan McCue <ethan at mccue.dev>
Sent: Saturday, June 28, 2025 12:05 PM
To: David Alayachew <davidalayachew at gmail.com>
Cc: amber-dev <amber-dev at openjdk.org>
Subject: Re: IO.println behavior on char[]
Yeah it's easy enough to work around, just noting the divergence
On Sat, Jun 28, 2025, 12:42 PM David Alayachew <davidalayachew at gmail.com<mailto:davidalayachew at gmail.com>> wrote:
Ah, I see you were referring to System.out.
Adding that overload makes sense. Though, I would rather it default to doing Arrays.toString, as opposed to just the one overload. That way, all arrays would be printable.
That was a common pain point for my students. Try to print an array of objects, and they only get an array address. And since they don't recognize that it's an address, it created some confusing bugs, as they think the hexadecimal address string was from previous or following print statements.
On Sat, Jun 28, 2025, 12:36 PM David Alayachew <davidalayachew at gmail.com<mailto:davidalayachew at gmail.com>> wrote:
Is there a char[] overload? I just checked the javadoc for 24 and 25, and I only see Object, String, and no-arg overloads. Not char[].
On Sat, Jun 28, 2025, 6:40 AM Ethan McCue <ethan at mccue.dev<mailto:ethan at mccue.dev>> wrote:
I was going through some examples in my writing and noticed that
IO.println(new char[] { 'a', 'b' });
will hit the Object overload of System.out.println and not the char[] overload
This breaks a few of my examples https://javabook.mccue.dev/arrays/aliasing
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20250629/d7cabfb7/attachment.htm>
More information about the amber-dev
mailing list