Friday, June 1, 2012

Efficiency of Java "Double Brace Initialization'?

In Hidden Features of Java the top answer mentions Double Brace Initialization , with a very enticing syntax:

Set<String> flavors = new HashSet<String>() {{
add("butter pecan");

This idiom creates an anonymous inner class with just an instance initializer in it, which "can use any [...] methods in the containing scope".

  1. Main question: Is this as inefficient as it sounds? Should its use be limited to one-off initializations? (And of course showing off!)

  2. The new HashSet must be the "this" used in the instance initializer ... can anyone shed light on the mechanism?

  3. Is this idiom too obscure to use in production code?

Summary: Very, very nice answers, thanks everyone.

  1. The generated code should run quickly. The extra .class files do cause jar file clutter, and slow program startup slightly (thanks to @coobird for measuring that). @Thilo pointed out that garbage collection can be affected, and the memory cost for the extra loaded classes may be a factor in some cases.

  2. Turned out to be most interesting to me. If I understand the answers, what's happening in DBI is that the anonymous inner class extends the class of the object being constructed by the new operator, and hence has a "this" value referencing the instance being constructed. Very neat.

  3. People felt the syntax should be clear (though I'd recommend an occasional comment, especially if your code will pass on to developers who may not be familiar with it).

Overall, DBI strikes me as something of an intellectual curiousity. Coobird and others point out you can achieve the same effect with Arrays.asList, varargs methods, Google Collections, and the proposed Java 7 Collection literals. Newer JVM languages like Scala, JRuby, and Groovy also offer concise notations for list construction, and interoperate well with Java. Given that DBI clutters up the classpath, slows down class loading a bit, and makes the code a tad more obscure, I'd probably shy away from it. However, I plan to spring this on a friend who's just gotten his SCJP and loves good natured jousts about Java semantics! ;-) Thanks everyone!

Source: Tips4all

No comments:

Post a Comment