Quick revision
The short answer
Because Java expresses unmodifiable collections as run-time behaviour of the ordinary List and Map interfaces, the type List never tells a caller whether add will work, so our contracts must say it. Guava's ImmutableList makes immutability visible in the type but ties every consumer of our API to Guava, and Kotlin callers see Java collections as platform types and may assume they can change them. The convention I would set is that all collections returned from our APIs are unmodifiable, enforced with List.copyOf, documentation and tests, and revisited only if the JDK ever adds richer collection types.
What to remember
In this answer, we saw that Java made unmodifiable collections a run-time property of the ordinary List and Map interfaces to stay compatible with existing APIs, so the type never tells a caller whether add will work. Guava's types make immutability visible but tie consumers to Guava, and Kotlin sees Java collections as platform types that may be treated as mutable. The convention that every returned collection is unmodifiable, implemented with copyOf and checked by documentation, contract tests and review, removes the guesswork, and we revisit it only if the JDK adds richer types.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Because Java expresses unmodifiable collections as run-time behaviour of the ordinary List and Map interfaces, the type List never tells a caller whether add will work, so our contracts must say it. Guava's ImmutableList makes immutability visible in the type but ties every consumer of our API to Guava, and Kotlin callers see Java collections as platform types and may assume they can change them. The convention I would set is that all collections returned from our APIs are unmodifiable, enforced with List.copyOf, documentation and tests, and revisited only if the JDK ever adds richer collection types.
Background to this question
When the JDK designers added List.of and List.copyOf, they chose not to create new ImmutableList types, so that the unmodifiable lists would work with every existing method that accepts a List. That keeps compatibility, but it pushes a question onto every API we write: will this list throw if someone adds to it? Without an organisation-wide answer, each team answers differently, and callers learn by exception. In this answer, we will look at what the JDK chose and why, the options it leaves us, how Kotlin callers see our collections, the convention I would adopt, and how to keep it honest.
1What the JDK Chose and Why
In Java, List is a single interface with optional operations. The Javadoc for add says that it may throw UnsupportedOperationException if the list does not support it. So an ArrayList, an Arrays.asList result, a Collections.unmodifiableList view and a List.of result all have the same static type, and the compiler cannot tell them apart. The difference only appears when the code runs.
The JDK designers chose this because a separate ImmutableList type would either have to extend List, inheriting add and so still throwing, or not extend it, making it unusable with the thousands of existing APIs that take a List. Either way it would split the ecosystem. Java 21's SequencedCollection interfaces kept the same approach. The price is that immutability is a property of the object, not of the type, and so it has to be stated in contracts.
2The Options This Leaves Us
The first option is to use a library type that makes immutability visible, such as Guava's ImmutableList, in our API signatures. A method returning ImmutableList<Order> tells every caller, and the compiler, that the result never changes, and Guava marks its mutating methods as deprecated so IDEs warn. The cost is a hard dependency: every consumer of our API must have Guava on the class path, at a compatible version, and Guava upgrades become our consumers' problem too. For a library used by many teams, that coupling is significant.
The second option is to keep java.util.List in signatures and make the behaviour predictable by convention. The type stays standard, and the knowledge moves into documentation, naming and tests. For most organisations this is the better fit, and it is what I would recommend for public APIs.
| Approach | Visible in the type? | Cost |
|---|---|---|
java.util.List, no convention | no | callers guess; failures at run time |
java.util.List plus a convention | no, but predictable | documentation, review and tests |
Guava ImmutableList in signatures | yes | every consumer depends on Guava |
| Kotlin List (read-only interface) | for Kotlin code only | Java callers still see java.util.List |
3How Kotlin Callers See Our Collections
Kotlin has separate read-only and mutable interfaces, List and MutableList, but both compile to java.util.List. When Kotlin code calls a Java method that returns java.util.List, the compiler cannot know which one was meant, so it shows a platform type, written (Mutable)List<String>! in error messages. Kotlin then lets the caller treat it as either.
So a Kotlin team may assume a Java list is mutable and only discover otherwise in production. JetBrains nullability and mutability annotations can help Kotlin infer better types, but the simplest protection is again a clear, consistent convention that Kotlin teams know about.
4The Convention I Would Set
The convention is short enough to remember: every collection returned from a public method is unmodifiable, unless the method's name and documentation explicitly say otherwise. Implementations return List.copyOf, List.of, Map.copyOf or Stream.toList() results, never internal mutable collections. Methods that accept collections never modify them; if a method needs to fill a collection, it returns a new one instead of taking an output parameter.
This removes guesswork in the common case. A caller who wants to change a returned list knows to copy it first with new ArrayList<>(result), and a caller who keeps it knows it will not change underneath them. The rare method that returns a live view or a mutable collection becomes the exception that stands out in review.
5Keeping the Convention Honest
Conventions decay unless something checks them. I would add a contract test helper that each library can use: it calls every public collection-returning method and asserts that add throws UnsupportedOperationException. Static analysis can catch fields of mutable collection types being returned directly. API review checks the Javadoc wording. And the convention goes into the onboarding guide for Kotlin teams, with a note to treat platform-typed collections from our libraries as read-only.
Finally, the decision should be revisited if the JDK changes. If a future Java release adds richer collection types or a standard way to mark immutability in the type, we would evaluate adopting it at a major version. Until then, documentation, tests and copyOf give us the predictability the type system does not.
- Does it return an internal mutable collection?Yesreject: store List.copyOf and return thatNocontinue
- Is it a live view or meant to be changed?Yesrequire name and Javadoc to say soNocontinue
- Javadoc states: unmodifiablethe default contract
- Contract test asserts add throwsruns in the library's build
4 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Why not just use Guava types everywhere internally?
Inside one service that already depends on Guava, it is a reasonable choice and makes intent clearer. The concern is public library APIs consumed by many teams, where the type leaks into every caller. A mixed approach, Guava inside, java.util.List at the boundary, is common and works well.
Should methods accept ImmutableList parameters to force callers to pass immutable data?
Usually not. It forces every caller to convert, and the method cannot rely on it anyway unless the elements are immutable too. It is simpler for the method to accept List and call List.copyOf if it needs to keep the data; copyOf costs nothing when the input is already unmodifiable.
What is the risk of the convention for callers that used to mutate returned lists?
Changing an existing method from returning a mutable list to an unmodifiable one is a breaking change for callers that added to it. I would apply the convention to new APIs immediately and to existing ones at a major version, with release notes and a search across known consumers first.
7Conclusion
In this answer, we saw that Java made unmodifiable collections a run-time property of the ordinary List and Map interfaces to stay compatible with existing APIs, so the type never tells a caller whether add will work. Guava's types make immutability visible but tie consumers to Guava, and Kotlin sees Java collections as platform types that may be treated as mutable. The convention that every returned collection is unmodifiable, implemented with copyOf and checked by documentation, contract tests and review, removes the guesswork, and we revisit it only if the JDK adds richer types.
Open with
Java decided that an unmodifiable list is still just a java.util.List, and add simply throws at run time. That kept every existing API that takes a List working, but it means the type tells a caller nothing about whether they can change what they got back.
Then, if they want more
So I think we have to put that information into contracts, and make it predictable. One option is Guava's ImmutableList in signatures, which makes immutability visible to the compiler, but in public libraries it forces every consumer to depend on Guava at a compatible version, which I would avoid at the boundary.
Kotlin adds another wrinkle: Kotlin's read-only and mutable lists both map to java.util.List, so a Java return value arrives as a platform type and Kotlin will happily let a caller treat it as a MutableList and fail at run time.
The convention I would set is that every collection returned from a public method is unmodifiable, implemented with List.copyOf or List.of, and documented as such, and anything that returns a live view or a mutable collection must say so in its name and Javadoc.
I would back it with a small contract test that asserts add throws, static analysis against returning internal fields, and a note for Kotlin teams. And if the JDK ever adds richer collection types, we would revisit it at a major version.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Java chose to express unmodifiable collections as run-time behaviour of the same List...": https://scrivio-interview-library.netlify.app/java/immutability/86f1898efa/