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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Have Generics Killed Java?

1 点作者 mellis将近 15 年前

3 条评论

gills将近 15 年前
Er...[if you have the misfortune of being paid to work in Java] they remove a pile of type-casting cruft and make code more useful to large and/or transient teams.<p>The author conveniently glosses over the type-casting from pre-generics Java and instead compares to Ruby. Duh...of course Ruby is a cleaner read. But compare learning a new API:<p><pre><code> // pretty evil List&#60;Thing&#62; getTheThings(); // really evil List getTheThings(); </code></pre> Or iteration:<p><pre><code> // pretty evil for (Thing t: getTheThings()) {...} // kill me now for (Iterator it = getTheThings().iterator(); it.hasNext()) { Thing t = (Thing) it.next(); ... }</code></pre>
mfukar将近 15 年前
They provide more type safety and not syntactic sugar. One cannot have complete type safety at runtime using type casts; if he thinks he can, he needs to take a simple programming course again.<p>If the OP wanted readability, he could use iterators. It's amazingly clear:<p><pre><code> private void printCollection(Collection c) { Iterator&#60;String&#62; i = c.iterator(); while(i.hasNext()) { System.out.println("Item: "+i.next()); } }</code></pre>
mhd将近 15 年前
There's just no real alternative to generics in Java. It's either that, casting (terminally insecure) or a more modern type system, which would be a huuuge change for the language (and in which case, you might as well use Scala).<p>If you want to complain about things that make code harder to understand, I wonder why the author doesn't rant about annotations.