TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Double Brace Initialization in Java

38 点作者 angeladur将近 13 年前

9 条评论

tzs将近 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.
评论 #4403841 未加载
评论 #4403822 未加载
评论 #4403693 未加载
评论 #4403866 未加载
philthom将近 13 年前
Check out the Immutable Maps feature, does the same with some nice syntactic sugar - <a href="http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/ImmutableMap.Builder.html" rel="nofollow">http://docs.guava-libraries.googlecode.com/git/javadoc/com/g...</a>
评论 #4403320 未加载
jfb将近 13 年前
This just makes me glad I don't have to write Java any more.
评论 #4403268 未加载
评论 #4404213 未加载
manojlds将近 13 年前
This seems to add more complexity than what it's worth. Non-final requirement and the equals problem, i'd rather ignore it all together!
LinaLauneBaer将近 13 年前
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?<p>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 on the number of classes you can have? At least it makes debugging problematic.<p>But I might be wrong about this since I am not a Java expert...
评论 #4403836 未加载
评论 #4404000 未加载
评论 #4404192 未加载
ansciath将近 13 年前
I consider this to be an antipattern, in part for some of the reasons listed in the article. The syntactic convenience being attempted here is more appropriately encapsulated as a utility method.
muyyatin将近 13 年前
It can also make UI code much more readable, but it also comes with the cost of making the bytecode much larger (each use of the double-brace initialization creates a new class).
chamakits将近 13 年前
I had known of this before, and I'm fairly certain I've used it before. However, one thing I didn't consider is the equals issue commented on the site. As attractive as it may be to use, I think from now on I will refrain from using the double braces.
评论 #4403267 未加载
J446将近 13 年前
Collection literals for inclusion in JDK8:<p><a href="http://jcp.org/en/jsr/detail?id=337" rel="nofollow">http://jcp.org/en/jsr/detail?id=337</a>