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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Why I absolutely love java

77 点作者 lfborjas超过 14 年前
not

31 条评论

redthrowaway超过 14 年前
I know it's fun to rag on Java, but this is a meaningless comparison. Java isn't a scripting language, and it wasn't ever intended to be one. It was never designed to solve quick little problems in a few lines, it was designed to deal with massive enterprise apps with complicated dependency trees over multiple architectures, and it does that job pretty damned well. Stop complaining that Java isn't Python/Ruby. It's like saying that x86 assembly is terrible because string concatenation takes so many loc.<p>Judging a language by standards it was never designed to meet is pointless. If you want to decide whether Java is a good language or not, see if it solves the problems it was designed to solve. I'd say it does. I may not like working in Java for small projects, but that doesn't make it a bad language.
评论 #2195550 未加载
评论 #2195564 未加载
评论 #2195528 未加载
评论 #2195717 未加载
评论 #2195480 未加载
评论 #2195491 未加载
评论 #2195533 未加载
评论 #2195762 未加载
评论 #2195464 未加载
评论 #2195510 未加载
评论 #2195757 未加载
评论 #2195911 未加载
评论 #2195795 未加载
评论 #2195578 未加载
评论 #2195841 未加载
sfraser超过 14 年前
To be clear I &#60;3 clojure, but I'm taking the bait just for fun:<p>1) The chunk of Java code shows a complete program - the others show function definitions 2) The Java is written purposely to be as verbose as possible 3) The Java sends it's output to stdout, unlike the others 4) The Java code is needlessly written using generics<p>Things the comparison ignores: 1) The Java code is more toolable than the others 2) The Java code will outperform most of the other examples once it has been subject to Hotspot<p>Here's a more fair comparison, at the function level to level the playing field (and only one import needed: java.util.*):<p><pre><code> List trollBait() { return new ArrayList(){{ for (int i : new int[]{1, 2, 3, 4}) { if (i % 2 == 0) add(i); }}}; } </code></pre> Here's why I absolutely do heart Java:<p>I just ran this 100,000 times in 6 milliseconds on a Core i7 920 (2.67 GHz). And I scanned it with Findbugs, and PMD, and a security vulnerability tool. And I refactored it in my IDE with crazy IntelliJ foo-jitsu. And it's more strongly typed. And it's runtime memory model is safer.
评论 #2195826 未加载
评论 #2195868 未加载
评论 #2195908 未加载
blinkingled超过 14 年前
If you want to love Java - love it for the tooling. IDEs are bloated but the integrated features they provide - like built in profiler(Netbeans), postmortem analysis tools (Eclipse MAT), the ease of debugging it provides - remote attach, JConsole type stuff where you can visualize loads of interesting stuff while the JVM is still executing your program, things like EProf that comes with HP JVMs - it can be turned on and off dynamically with low overhead and no changes required - the sort of data it gives makes it a piece of cake to figure out what is going wrong (it even tells you during the profile which exceptions were thrown and how many times!).<p>I make a living writing and supporting Enterprise Java apps and if it wasn't for these rich set of tools, my job would have been a nightmare. (Before flaming me saying if he wasn't writing code so badly in first place, he wouldn't need those tools - I also support code written by other people and writing complex, well performing code still remains notoriously hard.)
vilya超过 14 年前
Here's a C++ 0x version, using a lambda function:<p><pre><code> #include &#60;iostream&#62; #include &#60;algorithm&#62; int main(int argc, char** argv) { int src[] = { 1, 2, 3, 4 }; int* end = std::remove_if(src, src + sizeof(src) / sizeof(int), [](int i) -&#62; bool { return i % 2 != 0; }); for (int* p = src; p != end; ++p) std::cout &#60;&#60; *p &#60;&#60; " "; std::cout &#60;&#60; std::endl; } </code></pre> I wasn't sure about the lambda function syntax, but this compiles with g++ 4.5 (interestingly, even with the -Wall option, g++ doesn't warn about main not returning a value). Shows that even c++ can be shorter than java...
评论 #2197725 未加载
mike_esspe超过 14 年前
Scala<p><pre><code> List(1, 2, 3, 4).filter(_ % 2 != 0) </code></pre> And it's very easy to move to Scala from Java :)
julian37超过 14 年前
This can be a lot shorter using Google Guava [1]:<p><pre><code> Collections2.filter( Lists.newArrayList(1, 2, 3, 4), new Predicate&#60;Integer&#62;() { public boolean apply(Integer i) { return i % 2 == 0; } }); </code></pre> As I understand it, JDK 8 will support lambdas which will make it shorter still.<p>[1] <a href="http://code.google.com/p/guava-libraries/" rel="nofollow">http://code.google.com/p/guava-libraries/</a>
评论 #2195798 未加载
评论 #2195765 未加载
santadays超过 14 年前
<p><pre><code> import static com.google.common.collect.Lists.*; import static com.google.common.collect.Collections2.*; import com.google.common.base.*; public class AdmittedlyNotMuchBetter { public static void main(String[] args) { System.err.println(filter(newArrayList(1,2,3,4),new Predicate&#60;Integer&#62;() { public boolean apply(Integer input) {return input % 2 == 0;} })); } } </code></pre> btw, how do you preserve source indentation in the comments? [edit: formatting, thanks]
评论 #2195635 未加载
评论 #2195634 未加载
uvTwitch超过 14 年前
C#: var filtered = (new int[]{1,2,3,4}).Where&#60;int&#62;( i =&#62; i%2 != 0 );
评论 #2195576 未加载
评论 #2195536 未加载
评论 #2195679 未加载
评论 #2195570 未加载
rlmw超过 14 年前
-- Haskell<p>filter even [1,2,3,4]
评论 #2195490 未加载
评论 #2195471 未加载
评论 #2195472 未加载
nl超过 14 年前
Screw that.<p>Others have already pointed out the Google Guava library. This version is almost all just standard Java, just using Guava to create the list of numbers.<p><pre><code> ArrayList&#60;Integer&#62; list = com.google.common.collect.Lists.newArrayList(1,2,3,4); for (Iterator&#60;Integer&#62; it = list.iterator(); it.hasNext(); ) {if (it.next() % 2 == 0) {it.remove();}} System.out.println(list); </code></pre> Also, Java does have REPL. It's called the Eclipse scrapbook view. I'm sure Netbeans has something similar (and that is part of one of the standard Java SDK downloads).
评论 #2213055 未加载
评论 #2195892 未加载
评论 #2195872 未加载
eldenbishop超过 14 年前
-- Groovy<p><pre><code> (1..4).findAll{!(it % 2)}</code></pre>
评论 #2195752 未加载
评论 #2195554 未加载
dstein超过 14 年前
You cheated. Do it without the fancy schmancy lists.
srgseg超过 14 年前
Every time I see one of these ugly Java code examples, I always scratch my head because my code never looks like this.<p>Here is what these code examples are exploiting:<p>1. Java isn't designed for scripting. All of my Java programs require many classes. I hardly have any main() methods because most things I write are classes invoked by other classes, e.g. in the case of a servlet being invoked by the web application server.<p>2. It's rare in practice that I need to hard code the contents of data structures inside my own code. Normally they are loaded from data files.<p>3. Import statements are automatically thrown in by the IDE and most people hardly even notice they're there and type nothing more than Alt-Enter any time there is an import line missing.<p>So really, what I really am typing in java is only:<p><pre><code> List&#60;Integer&#62; result = new ArrayList&#60;Integer&#62;(); for(int i : source) if(i%=2) result.add(i); </code></pre> Now that's not really all that complicated is it?
评论 #2196014 未加载
democracy超过 14 年前
JAVA<p>for (int i:new int[]{1,2,3,4}) System.out.print(i%2==0?i:"");
评论 #2195792 未加载
andralex超过 14 年前
D rocks as usual: filter!`a % 2 == 0`(iota(1, 5)) (no eager computation, no dynamic allocation)
zackb超过 14 年前
Or you can just use groovy and get the best of both worlds :) [1,2,3,4].findAll {it % 2 == 0}
ichramm超过 14 年前
That's the problem of java developers, they dont use the bitwise operations:<p>if(e.intValue()%2 != 0) == &#62;&#62;&#62; if((e.intValue()&#38; 1) != 0)
评论 #2195636 未加载
bitsai超过 14 年前
I don't know about the other lisp dialects, but the anonymous function is actually not needed in the Clojure version:<p>(filter even? [1 2 3 4])
评论 #2195477 未加载
dekz超过 14 年前
<i>coffeescript</i><p>[1..4].filter (x) -&#62; !(x%2)
earino超过 14 年前
perl (because I feel like it):<p>grep { $_ % 2 == 0 } (1,2,3,4,5);
评论 #2195658 未加载
djacobs超过 14 年前
I used this exact example a few months ago to compare Java and Ruby [1]. Funny how it keeps coming up.<p>(While I was using Java, I never even knew there was a need for higher-order functions.)<p>[1] <a href="http://news.ycombinator.com/item?id=1947723" rel="nofollow">http://news.ycombinator.com/item?id=1947723</a>
daleharvey超过 14 年前
Erlang<p><pre><code> [X || X &#60;- [1,2,3,4], X rem 2 == 0]</code></pre>
评论 #2195722 未加载
评论 #2195742 未加载
va_coder超过 14 年前
What is Doug Cutting is saying these days about Java? He wrote Lucene, Hadoop and other awesome things in Java. I don't like Java much but I'd love to hear his side of the argument.
yakto超过 14 年前
Job security?
huherto超过 14 年前
<p><pre><code> public class filter { public static void main (String [] args) { System.out.println("2, 4"); } } </code></pre> FTFY, While it may be fun that show off with your favorite language, the Java version is intentionally complex.<p>Edit: In case you missed the point. This version is as absurd trying to be short as the other version is absurd trying to be long. Also, I can guarantee that an "Enterprise" version would be 200 lines long :-P
评论 #2195646 未加载
评论 #2195665 未加载
评论 #2195645 未加载
rll超过 14 年前
PHP<p><pre><code> array_filter(array(1,2,3,4),function($v) { return !($v&#38;1); });</code></pre>
waterhouse超过 14 年前
egrep:<p><pre><code> echo '1 2 3 4' | egrep -o '\&#60;[0-9]*[02468]\&#62;'</code></pre>
mitko超过 14 年前
Advice, drop the 1 and 3: just [2,4] - its cleaner.
jules超过 14 年前
J solution anyone?
评论 #2195681 未加载
fleitz超过 14 年前
F#<p><pre><code> [1..4] |&#62; List.filter(fun x -&#62; (x % 2) = 0) </code></pre> C#<p><pre><code> using System.Collections.Generic; public static class Foo { public static void Main(string[] args){ Enumerable.Range(1,4).Where(x =&#62; (x % 2) == 0); } }</code></pre>
foresterh超过 14 年前
No C#?
评论 #2195476 未加载
评论 #2195448 未加载