What is the difference between List.of() and Arrays.asList()?
Why Interviewers Ask This
This tests whether you can apply Java knowledge to real-world scenarios. Interviewers are looking for clarity of thought and evidence that you've encountered this in production code.
Answer
Arrays.asList() returns a fixed-size list backed by the original array — you can update elements (set), but cannot add or remove (throws UnsupportedOperationException). Changes to the list affect the original array and vice versa. It allows null elements. List.of() (Java 9+) returns a truly immutable list — any attempt to add, remove, or set throws UnsupportedOperationException. It does not permit null elements (throws NullPointerException). List.of() is preferred for creating small, permanent collections. new ArrayList<>(List.of(...)) creates a mutable copy. Collections.unmodifiableList() wraps any list in an unmodifiable view but the backing list can still be changed.
Pro Tip
Back up your answer with a specific project or situation. Saying 'In my last Java project, I used this when...' immediately makes your answer more credible and memorable.
More Java Questions
View all →- Intermediate What is the Java Collections Framework?
- Intermediate What is the difference between ArrayList and LinkedList?
- Intermediate What is HashMap in Java and how does it work internally?
- Intermediate What is the difference between HashMap and HashTable in Java?
- Intermediate What is the difference between HashMap and LinkedHashMap?