DEV Community

Discussion on: Java 15 in 2020: Reasons to *not* use Java?

 
brunoborges profile image
Bruno Borges • Edited
List.of(a, b, c)
       .forEach(it -> it = null); // mutate the data

This code doesn't change the data at all. The list continues to be the same after the forEach. This code above is the same as this:

        var a = "a";
        var b = "b";
        var c = "c";

        var list = List.of(a, b, c);
        list.forEach(it -> it = null); // mutate the data
        System.out.println(list);  // prints [a, b, c]

        for(var x : list) {
          x = null;
        }
        System.out.println(list); // prints [a, b, c]

The data continues to be immutable.

Thread Thread
 
michelemauro profile image
michelemauro

If you need val immutability, Scala is just a few jars away, And VSCode support with Metals is getting really good.