IO.println behavior on char[]
Remi Forax
forax at univ-mlv.fr
Sun Jun 29 15:47:08 UTC 2025
> From: "Chen Liang" <chen.l.liang at oracle.com>
> To: "Ethan McCue" <ethan at mccue.dev>, "David Alayachew"
> <davidalayachew at gmail.com>
> Cc: "amber-dev" <amber-dev at openjdk.org>
> Sent: Sunday, June 29, 2025 4:54:36 PM
> Subject: Re: IO.println behavior on char[]
> 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)
I agree,
arrays are fully weird in Java, they don't behave like proper objects (no equals/hahCode/toString), they have a weird hierarchy, they are always mutable and they do not work properly with generics too.
So they are not simple to use, they are low level primitives, not something you do not want introduce early in the learning process.
regards,
Rémi
> 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 < [
> mailto:davidalayachew at gmail.com | 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 < [
>> mailto:davidalayachew at gmail.com | 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 < [ mailto:ethan at mccue.dev |
>>> 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 |
>>>> https://javabook.mccue.dev/arrays/aliasing ]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20250629/dd0f622a/attachment-0001.htm>
More information about the amber-dev
mailing list