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.
Java Hack: Double Brace Initialization
11–20 of 22 posts
Re: Java Hack: Double Brace Initialization
#12Watch 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.
Re: Java Hack: Double Brace Initialization
#13Watch 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.
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
#14For initializing lists there is a less verbose expression in Java: List list = Arrays.asList(1,2,3,4,5);
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
#15For 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…
Re: Java Hack: Double Brace Initialization
#16Watch 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)…
Re: Java Hack: Double Brace Initialization
#17Watch 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.
Re: Java Hack: Double Brace Initialization
#18Watch 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.
Re: Java Hack: Double Brace Initialization
#19Watch 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
#20 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);