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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Java Generics FAQs

58 点作者 xvirk大约 10 年前

5 条评论

_lce0大约 10 年前
TIL why<p><pre><code> class Foo&lt;T&gt; { void test(Foo&lt;T&gt; f) { &#x2F;&#x2F; ... } static void boom() { Foo&lt;?&gt; f = new Foo&lt;String&gt;(); f.test(f); &#x2F;&#x2F; &lt;--- boom! compile error } }</code></pre>
评论 #9391981 未加载
评论 #9391968 未加载
评论 #9391775 未加载
评论 #9391824 未加载
uv3d779b大约 10 年前
I love Java-style generics! Any other popular languages have anything similar? Besides C# of course.
评论 #9391927 未加载
评论 #9392021 未加载
评论 #9391990 未加载
评论 #9392762 未加载
halosghost大约 10 年前
Public, bare getters&#x2F;setters! Hisss!!!!!<p>To elaborate, this is one of the most common annoyances I have with Java ecosystems. In an attempt to make everything Objectionable (ho-ho!), coders write classes with their internal data structure marked as private but then make getters and setters (which do nothing other than set or retrieve the value) which are public.<p>The result is that their internal data structure is actually public (since its advertised through the getters&#x2F;setters), but with the added bonus of extra function calls and member offset resolutions (i.e., it makes execution slower while single-handedly destroying the main benefit that OOP offers—backwards compatibility through isolation of the internal data structure).<p>While it doesn&#x27;t surprise me to see new-to-OOP coders making this kind of mistake, I find it troubling that many FAQs&#x2F;tutorials&#x2F;classes actually advocate for these patterns.<p>On the bright side, the web page looks nice :)
评论 #9392317 未加载
评论 #9392265 未加载
评论 #9392314 未加载
dcposch大约 10 年前
I think this illustrates one reason why Go doesn&#x27;t have generics.<p>Generics add <i>a lot</i> of complexity to a language. There are two main ways to implement them that I know of--the Java style, with type erasure, and the C++ style, where you essentially generate code for each instantiation--both have significant drawbacks. Either way, it complicates the syntax, complicates the type checker, complicates all the tooling around the language (IDE support, debugger, profiler, linter).<p>OTOH, if you don&#x27;t have generics, you either just use concrete types or you essentially do type erasure yourself, explicitly (eg with interface{} in Go or void* in C). It costs you a few casts and maybe some code duplication.<p>In return, you get a simple language, where it&#x27;s easier to write robust tooling.<p>I think erring on the side of simplicity and avoiding generics altogether is more than worth it.
评论 #9392682 未加载
评论 #9392757 未加载
DavidPlumpton大约 10 年前
I was dealing with this a couple of days ago: List&lt;List&lt;List&lt;Map&lt;String, Object&gt;&gt;&gt;&gt;<p>Sigh.