Live data from Hacker News

Java Hack: Double Brace Initialization

refactory.org

11–20 of 22 posts

Re: Java Hack: Double Brace Initialization

#11
post #3

Watch out - double-brace initialization is cool looking, but it's orders of magnitude slower than regular initialization as it must generate an anonymous class. Also, the generated class is put into the permgen space, which is not garbage collected. The permgen is pretty small by default - and if you fill it up, your system is hosed. I use them all the time in unit tests, but never in production code.

Seems wasteful, just to save a few keystrokes.

Re: Java Hack: Double Brace Initialization

#12
post #3

Watch out - double-brace initialization is cool looking, but it's orders of magnitude slower than regular initialization as it must generate an anonymous class. Also, the generated class is put into the permgen space, which is not garbage collected. The permgen is pretty small by default - and if you fill it up, your system is hosed. I use them all the time in unit tests, but never in production code.

[deleted]

Re: Java Hack: Double Brace Initialization

#13
post #3

Watch out - double-brace initialization is cool looking, but it's orders of magnitude slower than regular initialization as it must generate an anonymous class. Also, the generated class is put into the permgen space, which is not garbage collected. The permgen is pretty small by default - and if you fill it up, your system is hosed. I use them all the time in unit tests, but never in production code.

This is one case where I really prefer C#'s approach; it supports a shortcut syntax (works on anything which implements IEnumerable and has a public Add method):

  List intList = new List() { 1, 2, 3 };
which involves no anonymous classes or anything of the sort. The compiler merely "expands" it into the following before producing the bytecode:

  List intList = new List();
  intList.Add(1);
  intList.Add(2);
  intList.Add(3);
Which is, really, about what I'd expect such a shortcut syntax to do.

Re: Java Hack: Double Brace Initialization

#14
post #7

For initializing lists there is a less verbose expression in Java: List list = Arrays.asList(1,2,3,4,5);

. . . except that doesn't get you a modifiable list, it just wraps a List interface around the array that's implicitly created by the var-arg, so it's a trick you have to be careful with.

Personally I often end up writing little helpers like: List list(T... args) { return new ArrayList(Arrays.asList(args)) } usually in test code. A built-in List and Map-initialization syntax is really something every decent programming language ought to have.

Re: Java Hack: Double Brace Initialization

#15
post #14
post #7

For initializing lists there is a less verbose expression in Java: List list = Arrays.asList(1,2,3,4,5);

. . . except that doesn't get you a modifiable list, it just wraps a List interface around the array that's implicitly created by the var-arg, so it's a trick you have to be careful with. Personally I often end up writing little helpers like: List list(T... args) { return new ArrayList (Arrays.asList(args)) } usually in test code. A built-in List and Map-initialization syntax is really something every decent programm…

(Consider using the Google collections library if possible. It already has that in the form of classes like Lists, Maps, etc that provide builders and factory methods.)

Re: Java Hack: Double Brace Initialization

#16
post #3

Watch out - double-brace initialization is cool looking, but it's orders of magnitude slower than regular initialization as it must generate an anonymous class. Also, the generated class is put into the permgen space, which is not garbage collected. The permgen is pretty small by default - and if you fill it up, your system is hosed. I use them all the time in unit tests, but never in production code.

This is one case where I really prefer C#'s approach; it supports a shortcut syntax (works on anything which implements IEnumerable and has a public Add method): List intList = new List () { 1, 2, 3 }; which involves no anonymous classes or anything of the sort. The compiler merely "expands" it into the following before producing the bytecode: List intList = new List (); intList.Add(1); intList.Add(2); intList.Add(3)…

[deleted]

Re: Java Hack: Double Brace Initialization

#17
post #3

Watch out - double-brace initialization is cool looking, but it's orders of magnitude slower than regular initialization as it must generate an anonymous class. Also, the generated class is put into the permgen space, which is not garbage collected. The permgen is pretty small by default - and if you fill it up, your system is hosed. I use them all the time in unit tests, but never in production code.

I think just generally you have to be aware that it is not a concrete ArrayList (or whatever) that is being created. You have to be careful about which classes you are anonymously subclassing. If the superclass has an equals method, for instance, that follows the getClass().equals(compare.getClass()) approach as part of its logic you are screwed.

Re: Java Hack: Double Brace Initialization

#18
post #3

Watch out - double-brace initialization is cool looking, but it's orders of magnitude slower than regular initialization as it must generate an anonymous class. Also, the generated class is put into the permgen space, which is not garbage collected. The permgen is pretty small by default - and if you fill it up, your system is hosed. I use them all the time in unit tests, but never in production code.

And it can break equals() if you're not careful.

Re: Java Hack: Double Brace Initialization

#19
post #3

Watch out - double-brace initialization is cool looking, but it's orders of magnitude slower than regular initialization as it must generate an anonymous class. Also, the generated class is put into the permgen space, which is not garbage collected. The permgen is pretty small by default - and if you fill it up, your system is hosed. I use them all the time in unit tests, but never in production code.

Seems wasteful, just to save a few keystrokes.

It's pretty useful in unit tests, since it very clearly lays out the sample data you'll be comparing. I rarely if ever used it in the production code.

Re: Java Hack: Double Brace Initialization

#20
I've been using this for years. It is a fantastic way to build up hierarchies. XML, maps, JSON, UI code etc. It is very concise, fast and produces clean code which also just happens to correctly indent when you apply code formatting.

  School s = new School() {{
    add(new Student() {{
      name = "Bobby";
      age = 15;
      add(new Pet() {{
        name = "Fluffy";
      }});
    }});
  }};

  vs.
  School sc = new School();
  Student st = new Student();
  st.name = "Bobby";
  st.age = 15;
  Pet p = new Pet();
  p.name = "Fluffy";
  st.add(p);
  sc.add(st);
Post reply on HN