Hacker News new | ask | show | jobs
by jfb 5059 days ago
This just makes me glad I don't have to write Java any more.
2 comments

> This just makes me glad I don't have to write Java any more.

You don't have to do it even if you are writing Java. This is a perfect example of clever for the sake of being clever.

The issue of course is that both the clever way and the 'normal' way are not exactly pleasant compared to most other modern languages.
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.

> List<String> myList = Arrays.asList("one", "two", "three");

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.