r/java • • Feb 04 '26

Ask the Java Architects with Brian Goetz and Viktor Klang (Jfokus 2026)

https://youtu.be/HB56zU3pTmQ
54 Upvotes

30 comments sorted by

View all comments

Show parent comment

2

u/aoeudhtns Feb 06 '26 edited Feb 06 '26

You actually don't need that. If you look at copyOf implementation, it already detects its internal unmodifiable wrapper and then just passes it back out if it's already that. So, feel free to defensively copyOf as much as you want!

Now, if you're talking about some sort of universal JDK feature, then that would be a different beast.

2

u/8igg7e5 Feb 06 '26

I was acknowledging that behaviour already - but it only works for unmodifiable types inside the JDK. Those aren't the only Collection implementations - and it doesn't support the Collections.unmodifiableList(...) wrappers.

At the moment we see a lot of unwanted and unnecessary list-copying. It would be good for the developer to be able to express that this list is safe to skip defensive copies without having to make a copy with List.of (paying a toll every time just in case the list might be passed to someone who wants the safety of a stable list)

1

u/aoeudhtns Feb 06 '26

I'm sorry. My eyes jumped right over that 3rd para/sentence. Too eager to get to the codeblock.