Live data from Hacker News

Double Brace Initialization in Java

c2.com

21–28 of 28 posts

Re: Double Brace Initialization in Java

#21

I am no Java expert but they write that this syntax automatically creates a anonymous inner class - every time you create a collection instance in this way. Or am I wrong about this? Most of the examples given are creating static objects but the last example a non-static object is created in the same way. Imagine you are doing this a couple of times then you end up with a lot of anonymous classes. Isn't there a limit…

In the majority of cases where you are deploying your java code on a server you own, having a few dozen or hundred extra classes is not a big deal. Each adds maybe a few kb to the size the jar, and as for loading costs, a class is only loaded into a running JVM once.

The cases in which to worry about stuff like this is if you need to load the code on someone else's machine, like with an applet or Android.

Re: Double Brace Initialization in Java

#22
post #13

I know this is blasphemy in the Java world, but I'd probably use a preprocessor to transform some clean syntax into the normal Java code. I'd rather have the slight increase in build complexity over the potential issues with the extra classes and the breaking of assumption about how things compare and such.

> I'd probably use a preprocessor to transform some clean syntax into the normal Java code. If I were in your place, I'd just learn enough Java to be proficient. Java is a very powerful language and certainly better suited for mundane tasks than your preprocessor.

I think you misunderstood; he wants to avoid doing mundane tasks.

There's a couple of extensible preprocessors that aim to do exactly this. They're called 'clojure', 'scala', and 'kawa'.

Re: Double Brace Initialization in Java

#23

I am no Java expert but they write that this syntax automatically creates a anonymous inner class - every time you create a collection instance in this way. Or am I wrong about this? Most of the examples given are creating static objects but the last example a non-static object is created in the same way. Imagine you are doing this a couple of times then you end up with a lot of anonymous classes. Isn't there a limit…

It's one class per use of this technique. It's not redefined every time the code runs.

Re: Double Brace Initialization in Java

#25
post #4

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

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 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.

Re: Double Brace Initialization in Java

#26
post #25
post #4

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

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 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"] The…

> List 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 myList = new ArrayList(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.

Re: Double Brace Initialization in Java

#27

I am no Java expert but they write that this syntax automatically creates a anonymous inner class - every time you create a collection instance in this way. Or am I wrong about this? Most of the examples given are creating static objects but the last example a non-static object is created in the same way. Imagine you are doing this a couple of times then you end up with a lot of anonymous classes. Isn't there a limit…

In the majority of cases where you are deploying your java code on a server you own, having a few dozen or hundred extra classes is not a big deal. Each adds maybe a few kb to the size the jar, and as for loading costs, a class is only loaded into a running JVM once. The cases in which to worry about stuff like this is if you need to load the code on someone else's machine, like with an applet or Android.

Isn't this what jars are suppose to make transparent and simple?

Re: Double Brace Initialization in Java

#28
post #17
post #13

I know this is blasphemy in the Java world, but I'd probably use a preprocessor to transform some clean syntax into the normal Java code. I'd rather have the slight increase in build complexity over the potential issues with the extra classes and the breaking of assumption about how things compare and such.

Something like Xtend ( http://www.eclipse.org/xtend/ ) ?

Never heard of this. Thanks
Post reply on HN