Details Explanation of java.util.List enhancement in Java 9

The List.of() static factory methods provide a convenient way to create immutable lists. The List instances created by these methods have the following characteristics:

  • They are structurally immutable. Elements cannot be added, removed, or replaced. Calling any mutator method will always cause UnsupportedOperationException to be thrown. However, if the contained elements are themselves mutable, this may cause the List’s contents to appear to change.
  • They disallow null elements. Attempts to create them with null elements result in NullPointerException.
  • They are serializable if all elements are serializable.
  • The order of elements in the list is the same as the order of the provided arguments, or of the elements in the provided array.
  • They are value-based. Callers should make no assumptions about the identity of the returned instances. Factories are free to create new instances or reuse existing ones. Therefore, identity-sensitive operations on these instances (reference equality (==), identity hash code, and synchronization) are unreliable and should be avoided.
  • They are serialized as specified on the Serialized Form page.

This interface is a member of the Java Collections Framework.


public class ListImmutableFactoryDemo{
  public static void main(String ... args){
    java.util.List<String> alphabet = java.util.List.of("Java", "C", "C++");
    for(String alp:alphabet){

 It will not support following things:

1.List.of method return immutable List object so modification is not possible

java.util.List<String> alphabet = java.util.List.of(“Java”, “C”, “C++”);


Above code will throws UnsupportedOperationException becuase List.of method will return immutable object so once object created modification is not possible on that object.

Exception in thread "main" java.lang.UnsupportedOperationException
        at java.base/java.util.ImmutableCollections.uoe(Unknown Source)
        at java.base/java.util.ImmutableCollections$AbstractImmutableList.add(Unknown Source)
        at Demo.main(

2. List.Of() method does not allowed null element

java.util.List<String> alphabet = java.util.List.of(“Java”, “C”, “C++”, null);

Above code will throws NullPointerException because as per java9 Documentation List.Of() method does not allowed null as value.

Exception in thread "main" java.lang.NullPointerException
        at java.base/java.util.ImmutableCollections$ListN.probe(Unknown Source)
        at java.base/java.util.ImmutableCollections$ListN.<init>(Unknown Source)
        at java.base/java.util.List.of(Unknown Source)
        at Demo.main(

Was this post helpful?

Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *