IO.println behavior on char[]
Ethan McCue
ethan at mccue.dev
Sun Jun 29 17:13:52 UTC 2025
My (formal) Java education started with a record-less version of the
following as the collection mechanism of choice
sealed interface LList<T> {
int size();
}
record Empty<T>() implements LList<T> {
@Override
public int size() {
return 0;
}
}
record NotEmpty<T>(T first, LList<T> rest) implements LList<T> {
@Override
public int size() {
return 1 + rest.size();
}
}
With every operation being defined recursively and (to start) every list
implementation being bespoke without generics.
Nowadays I would think this would be easier to teach since it would be
expressible without instance methods.
sealed interface LList<T> {}
record Empty<T> () implements List<T> {}
record NotEmpty<T>(T first, LList<T> rest) implements LList<T> {}
int size(LList<T> list) {
return switch (list) {
case Empty() -> 0;
case NotEmpty(_, rest) -> 1 + size(rest);
};
}
Which is all to say that I think there are *some* benefits to starting from
a way you "have to unlearn" later.
One pro of arrays is that they are mutable and, in the journey that starts
with standard loops instead of recursion, it functions well to demonstrate
mutable aliasing. The cons are what you are all describing.
On Sun, Jun 29, 2025, 11:52 AM Brian Goetz <brian.goetz at oracle.com> wrote:
>
>
> 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.
>
>
> Preach it, brother!
>
> I understand the tendency to want to teach "from the ground up", but
> starting with arrays (like with static methods) is just teaching habits
> that need to be unlearned.
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20250629/df4bccce/attachment.htm>
More information about the amber-dev
mailing list