|
|
|
|
|
by J446
5059 days ago
|
|
That depends if you mean Java the language or Java the platform. Java the language is to include immutable collection literals in JDK8: http://jcp.org/en/jsr/detail?id=337 Today Java lets you do the following: List<String> myList = Arrays.asList("one", "two", "three"); Scala and Groovy which target the JVM let you do things like: Scala:
val myList = List("one", "two", "three") Groovy:
def myList = ["one", "two", "three"] Then there's JRuby, Jython etc. |
|
This is often not good enough, as Arrays.asList returns some internal immutable List type. If you then tried to do something like myList.add("four"); you'd get an exception.
What you'd actually need to do is something more like:
> List<String> myList = new ArrayList<String>(Arrays.asList("one", "two", "three"));
Compare that to more dynamic languages, where that example would be:
> myList = ["one", "two", "three"]
Yes, it's a trivial example, but it's something many programs involve all over the place, and the boilerplate adds up a tedious task.