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.
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.
> It was designed to deal with massive enterprise apps with complicated dependency trees over multiple architectures
Actually it was designed to run on washing machines, refocused to work for browser applets, and then refocused again to deal with massive enterprise apps. I'm not sure it was ever suited to any of those tasks -- it's merely has a good enough cross section of features. One only has to look over at what Microsoft has done with C# to see that Java could be significantly less verbose.
I thought the example was a little terse, he didn't even bother to include an XML configuration file for his IOC container, or a bunch of maven dreck to import a couple libraries. He also forgot to name the file correctly so it might not even compile. As well the batch file/shell script so it will actually execute and pass a bunch of arcane -XMemory flags to java.
I know it's fun to rag on Roman numerals, but they were never intended for totaling receipts. They were never designed for quick little sums, they were designed for engraving on massive long lasting objects like buildings and copyright notices on feature films, and they look pretty darned good there. Stop complaining that Roman numerals are not Arabic numerals. It's like saying counting on fingers is terrible because nobody has more than X them (That's 10 to you youngsters, or 0b1010 to you real smart alecks).
Judging a numeral by a standard it was never designed to meet is pointless. If you want to decide whether Roman numerals are a good number system or not, see if you can hammer them onto a building. I'd say you can. I may not like working with Roman numerals for grocery receipts, but that doesn't make them bad.
> They were never designed for quick little sums, they were designed for engraving on massive long lasting objects like buildings and copyright notices on feature films, and they look pretty darned good there. Stop complaining that Roman numerals are not Arabic numerals.
Actually, Roman numerals were designed for counting the men in your army, whereas Arabic numerals were designed for mathematics. Comparing the two isn't a good argument against redthrowaway's point at all, because both are better at the task they were designed for.
Are you saying that Roman numerals are better for counting than Arabic numerals? I suppose you don't mean count (I, II, III, IIII, V...), but show the number of men in the army. I don't see any advantage in using Roman numerals.
Lets say we divide infantry in XXX maniples of CXX men each, supposing they are complete and you haven't lost people yet in battle. How is that better than 30 maniples of 120 men?
Actually, roman numerals are easier to sum than arabics. To sum two roman numerals you just write all the digits together and collapse them as much as possible. There is no carrying over of digits and such.
Otoh, arabic numerals are a lot easier to multiply/divide. Before the wide adoption of arabic numerals all but the most educated couldn't perform simple multiplication/division.
Complain if you must, but there is a serious point involved. People get invested in technology and they start to adopt certain patterns of argument that stem more from their own investment than anything else. I tried to highlight the pattern using humor. Nothing personal intended.
In real life I do dislike java and try hard to avoid having to use it, precisely for the reasons highlighted in the OP. I do like Haskell, and many other things. Note: all the real Haskellers are nice and would never be as snarky as I was.
Well, at least the JVM was. Java the language was designed to lure C++ shops and not scare them too much. So in that respect, it was designed to be an enterprise class language.
It's perfectly reasonable to rag on it for this. What's not reasonable is that the code can be written in a much more compact way by not trying for an "applicative" style.
List even = new ArrayList();
for(int i : new Integer[]{1,2,3,4})
if (i%2==0) even.add(i);
Still larger than necessary, and this doesn't have all the necessary class cruft, but still much smaller.
Many of the other languages write this in functions that are first class citizens. The functions are ready to be used. In Java you would still need to put this inside another class to make it run and other classes would need to instantiate that class to run it.
This is admittedly better, note, tough, that I was trying to do it in the mindset of the other languages included (just having one explicit list and filtering out instead of in); however, as it was stated before, it's never good to try to write code in the idiomatic style of other languages if the end result is something not idiomatic at all, it's like trying to play the bach chaconne for violin in a tuba or <put better analogy here> : it can be done but it's just forcing it.
Where it all went wrong was when someone said Java is what the web should be developed with, we then got JSP, servlets and later Struts. Java web was brain dead on arrival and thankfully new style development trends are rectifying that evil. With JavaScript and Mobile apps taking over the UI from Java (as well as the other web frameworks for that matter), it becomes evident that Java when weighed in the balance is not that wanting, sure it does not do some things well and it can be a bit verbose, but as a back end language, I see no reason to abandon Java for purely technical merits. I do have concerns about Oracle's stewardship but that is another topic for another day.
However, since Java was not intended to be a scripting language, then it should never have monopolized the material of introductory programming classes like it did. I consider Java to be a very ungentle introduction to programming when compared with Python, Ruby or Scheme.
I agree, and my university is currently doing a 2-year A/B test where half the undergrads will get intro CS in Java, and the other half in Python. After the two years are up they'll see what group performs better in 3rd year classes. I think it's a great idea.
My 1st year profs were all fantastic, and the intro Java courses that I took did a fairly good job scaling from Hello World and FizzBang to a visual calculator processing pre-, in-, and post-fix arithmetic in a binary tree, so it wasn't too bad. Granted, first year was really easy and passed most students. Second year has, iirc, a 60% pass rate, and the program overall has a 40% pass rate. I'm not sure if that means it's rigorous or the profs are lousy, but I at least feel it means my degree is worth something when I graduate.
Emphasis added. Ensuring that students have a sound grounding in the basics and can then focus on the new material in later classes seems far more relevant to the goal of teaching computer science.
Universities focusing on throughput and employability is the problem, not the solution. It shouldn't be 4 more years of high school ...
That's true, and although I don't hate java or the technologies it has spawned (the JVM is a neat one, just look at it's finest offspring, clojure), trying to teach basic programming with it just seems bad. Maybe for an intermediate course on OOP, it does a good job if one wants to teach C++ or C afterwards; but as a first language, I have against it that it forces to spend too much time in syntax and inexplicable cruft (unless you're willing to explain the concept of class and static method the first day), and magic is never good for teaching if it's never explained. Just look at the classic SICP lectures: the syntax is done with in half an hour, then recursion and then higher order functions, and it doesn't feel like a brainfuck at all!
It actually happened to me, my first programming language in college was java and it made programming look really hard. I actually hated programming until the very end of my third year, when I learnt python in my first job. Ironically, later, when taking a compilers course -when I already knew how to program- I was quite happy with java+flex+cup, it felt powerful and fast.
Sorry to be pedantic, but wasn't Java designed initially for set-top boxes, when it was still called Oak, and was repeatedly re-purposed for running code in web pages (as applets) and then for enterprise-friendly server-side web apps (J2EE)?
> Java isn't a scripting language, and it wasn't ever intended to be one.
First class functions aren't a feature particular to scripting languages. Many compiled languages also have them, such as Haskell, Ocaml, Scala, F#, C#, D, Go, Forth, Common Lisp, and so on.
> It was never designed to solve quick little problems in a few lines
C# wasn't either, but it manages to be pretty expressive for what it is. I don't think you can argue Java's design precludes it from having basic things like first class functions, when (a) C# operates in the same market and already has them, and (b) Java 7 is going to be getting them.
This is not a meaningless comparison; it points out one of the main deficiencies of java: lack of first-class functions. The closest you can come is using anonymous inner classes, which is kind of kludgy.
Oh, this is just a tongue-in-cheek joke; java has really cool stuff, I actually code in java every morning and I really like the JVM and the JIT, without it, clojure, jruby and jython wouldn't exist, and jruby is way cooler than Cruby!
You're right, we can't be saying that a language sucks because it doesn't look cool in some coding styles. Is like trying to do fancy OOP in pure scheme or common lisp without the CLOS!
I know, and I both get and appreciate the joke. There are, however, a lot of people who think Java's (substantial) verbosity is a legitimate complaint against a language intended to do what it does. It's really neat to be able to accomplish something cool with list comprehension in Python, but when you're working on a massive codebase that might have 60 people working on the same code, it's nice to have the abstraction that Java was designed for, and the most basic syntax that a C-like language will allow.
There are, however, a lot of people who think Java's (substantial) verbosity is a legitimate complaint against a language intended to do what it does.
It is a legitimate complaint. Having to write loads of boilerplate makes the code more difficult to read and understand, and increases the surface area for bugs to sneak in. That's true for both one-off scripts and huge enterprisey systems.
Yep, in an environment like that the serious architecture java enforces is a good thing, especially with heterogenous groups of not-necessarily-hackers-but-good-coders.
I completely agree, though this is where I take issue with both the Java community and with managers and architects who have pushed for Java to be standard outside of the enterprise realm. I don't hate the language, I dislike the people who try to fit the square plug into the round hole. It's taking a lot of explaining to a lot of people at my workplace/university (a Java school naturally) that when developments teams are segregated into 1/2 person teams, have high turnover, and spread widely across an organization, using Java just doesn't make sense. It makes a lot more sense to use an easily approach scripting language such as Python or PHP in this case. But yet I had several Java evangelists literally screaming at me over our decision to go with PHP. In the end we won out, we built a solution that has run flawlessly (knock on wood) and the Java evangelists were moved to other departments.
Filtering a list of numbers by a criterion is not a standard. It's a simple task. The discussion does have relevance - it shows why Java programmers need to rely so heavily on cookie cutter libraries: writing something from scratch is a huge bear.
I routinely interview programmers of all kinds. Extremely few Java programmers ever make it because they simply don't know how to get simple (but non-mainstream) algorithmic tasks done.
Actually judging a language by standards it was never designed to meet is not pointless. I judge languages by how well they solve problems I need to solve. I don't care if it was intended to solve that problem.
Generally, typing extra stuff to make my code work isn't one of the problems I have.
x86 assembly IS terrible because string concat takes many loc. I use x86 assembly to solve problems where speed is paramount, string concatenation generally isn't one of those problems. Like lets say you need to reformat a little endian RGB pixel into a big endian RGB pixel, then that's something I'd use assembly for.
Java as a language is horrible language because the only problem it solves well is how to inflate a consulting bill, or how to waste developers time figuring out what exceptions a method throws. Only exception a method needs to throw is JavaOutTheDoor.
Solving a problem in java is like winning a race where your legs are hobbled, yes, it's an accomplishment, but wouldn't it have just been smarter to untie your legs? Java was invented in 1994 or something like that, and the scary part is it hasn't changed. Perhaps I should still code my C K&R style?
"Java as a language is horrible language because the only problem it solves well is how to inflate a consulting bill, or how to waste developers time figuring out what exceptions a method throws. Only exception a method needs to throw is JavaOutTheDoor."
You hover your mouse pointer over the method call and a pop-up window tells you what exceptions it can throw.
Better yet, right click the method call and tell it to insert the try and catch block(s) for you.
This is how professional Java developers work. It only takes a few seconds.
I find this sort of argument frustrating. "Java doesn't solve the problems I want to solve in the way I want to solve them, therefore Java is a horrible language and shouldn't be used by anyone." Fine, Java isn't the language for you; but other people have different problems or different ways to solving those problems and some of those people find Java to be a perfectly good language. Just because you don't like it doesn't mean everyone should hate it. To each his own.
> Java isn't a scripting language, and it wasn't ever intended to be one.
One of my beliefs is that we shouldn't think about "scripting languages" versus "compiled languages" and just think in terms of what would make for the ideal programming language, period. By that standard, Java is inferior to Python (to pick just one example) because it is so much more verbose and formal when accomplishing the same desired task.
To be clear I <3 clojure, but I'm taking the bait just for fun:
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
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
Here's a more fair comparison, at the function level to level the playing field (and only one import needed: java.util.*):
List trollBait() {
return new ArrayList(){{
for (int i : new int[]{1, 2, 3, 4}) {
if (i % 2 == 0) add(i);
}}};
}
Here's why I absolutely do heart Java:
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.
now we're talking, this is a really good response! I almost agree with all of your points, except for the performance, and only in thoroughness: a benchmark will be nice.
And for clarity: the other ones aren't function definitions, they also build a new list, like your example. The examples are meant to be run in a REPL and that's why they don't send to stdout, although it will be just another function application in most cases (but your example works in beanshell, the stand-alone java quasi-REPL, so we're ok). I think all of them can be considered a full-program in those languages: as a stand-alone chunk of code that can be compiled/interpreted and will produce an executable output, that is. And, yes, the autoboxing was the only reason I chose generics, but I tried for it to be concise, given my limited knowledge of java; the max-verbose mode would have used a Runnable or something evil to simulate a higher order function.
Thanks... my experience with clojure on the JVM is it is in general slower than optimally tuned Java code. I really can't imagine that any of the other languages you listed would easily outperform the code I have here, but maybe since the use-case is a sweet spot for functional? The person who responded to me with the F# example reported a crazy low time, and I am wondering if they have a defect in the code. I think it is possible a true functional language could make some tweaks that Java can't (like realizing the function is idempotent and caching the response instead of recalculating it). Maybe that is why the F# example was so quick - either the way it times itself is defective, or perhaps the runtime memoizes the function.
My experience with all the languages in your sampler is that their runtime performance usually lags the Java on the JVM for general purpose programming. This is not an argument not to use them - I would prefer many of them in certain use cases. But if I want a managed runtime, and have some performance critical code that I am OK with being written in an imperative language, it is hard to beat the performance of simple Java on the JVM. There has been so much tuning to Hotspot over the last decade. That's been my experience anyways.
Interesting, and yes, I am a witness of how fast proper java is; and in extreme performance cases, I guess I'd turn to java before C++ ; maybe the f# also benefits from optimizations (like tail-call optimization) and the underlying VM? (though I don't know how performant the CLR is)
That's not bad, how about 0.014 milliseconds for 100,000 iterations? On a Core i5 @ 2.6 Ghz
// Learn more about F# at http://fsharp.net
open System
let consume x = ()
let start = System.Environment.TickCount
Seq.init 100000 id
|> Seq.iter (fun x -> [0..4] |> List.filter (fun y -> (y % 2) = 0) |> consume )
let endTime = System.Environment.TickCount
printfn "%f milliseconds" (TimeSpan.FromTicks(int64(endTime - start)).TotalMilliseconds)
Output:
0.014100 milliseconds
Press any key to continue . . .
I'm sure if I added some java line noise I could get it up to 6 milliseconds. Too bad I didn't need a calendar class to figure out how to get from ticks to milliseconds. It's also really unfortunate that System.Environment.TickCount compiles down to a single assembly instruction. I was really hoping to use a factory pattern in there somewhere. Maybe some IOC containers. What good is a language if you can't write half your code in XML.
That is crazy fast... funny, but based on your response I am guessing you don't know why. What could possibly explain such a big difference? Understanding this is what separates a seasoned pro from someone throwing out trollbait comments about XML and assembly instructions.
First off, the resolution of TickCount is not less than 500 milliseconds. Are you sure you are actually timing it right? Maybe you should have used DateTime.Ticks which has a higher resolution. (seriously - I think you need to check your code)
But in any case, let's say your test is actually valid. You mentioned "Java line noise" - not really a real explanation for the difference.
So let me guess for you - maybe your F# runtime knows the function is pure and idempotent, and after running it once it probably just cached the output. So your run doesn't actually execute the filter across the list for every iteration, whereas my Java code did.
I could rewrite my Java code to return the same array over and over after it has been computed once, but it sure won't look as clean as a functional language like F#. I grant you that, and that's one of the reasons I prefer functional languages to Java.
Next time maybe put a little more thought into the response if you actually really have no idea why your program said "0.014100 milliseconds".
TickCount has 1 millisecond accuray and it's actually milliseconds, which is what tipped me off to the low numbers. It actually takes 140 milliseconds to execute. I redid the code to not use Seqs and now it's down to 15 ms.
// Learn more about F# at http://fsharp.net
open System
let consume x = ()
let start = System.Environment.TickCount
let arr = [| 1..4 |]
for i = 1 to 100000 do
arr
|> Array.filter (fun y -> (y % 2) = 0)
|> consume
let endTime = System.Environment.TickCount
printf "%d milliseconds" (endTime - start)
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!).
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.)
#include <iostream>
#include <algorithm>
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) -> bool { return i % 2 != 0; });
for (int* p = src; p != end; ++p)
std::cout << *p << " ";
std::cout << std::endl;
}
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...
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.
ArrayList<Integer> list = com.google.common.collect.Lists.newArrayList(1,2,3,4);
for (Iterator<Integer> it = list.iterator(); it.hasNext(); ) {if (it.next() % 2 == 0) {it.remove();}}
System.out.println(list);
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).
The lack of closures is seriously felt when creating the "isEven" Predicate. This will be a lot cleaner next year (?), when closures are introduced in the language. For now, we have to live with the verbosity...
It's possible to hide some of this verbosity and write clean code. For example, I often extract the function / predicate declarations to utility classes to avoid polluting the code. In this case, I would move the "isEven()" method to a "MathPredicates" utility class. The code ends up looking like:
List<Integer> list = newArrayList(1,2,3,4);
System.out.println(Iterables.filter(list, MathPredicates.isEven()));
// create an array list with 4 integer elements
ArrayList<Integer> list = com.google.common.collect.Lists.newArrayList(1,2,3,4);
// get an Iterator from the list, and use it to iterate though the list
for (Iterator<Integer> it = list.iterator(); it.hasNext(); ) {
// check if this element is even.
// If we want the odd numbers, change to != 0
if (it.next() % 2 == 0) {
// remove it if it is even
it.remove();
}
}
// print the list
System.out.println(list);
The commenter said they could almost figure out what it's doing. It's not clear from that that comment if it is just the formatting that was the problem (which I agree, isn't very readable).
This is true, I forgot to mention that the haskell version trivially scales to infinite lists. You can use partial application to encourage code reuse as well:
I'm currently in the process of learning it, and I'd definitely recommend it.
If nothing else, it's great having a compiler that can catch a large amount of errors without getting bogged down in the type boilerplate that many other static languages require.
Implicit static typing is definitely something I miss when working in other languages.
Every time I see one of these ugly Java code examples, I always scratch my head because my code never looks like this.
Here is what these code examples are exploiting:
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.
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.
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.
So really, what I really am typing in java is only:
List<Integer> result = new ArrayList<Integer>();
for(int i : source) if(i%=2) result.add(i);
The point is that you have to write the for loop and if statement and concatenation for each different function you want to define. The better way is to use higher order functions, which Java lacks.
There's no problem here. You can be pretty sure simple calculations like this (and multiplications with a power of 2 vs. shifting etc.) will run the same machine code in the end.
I think you are right in this case - I updated my example code above to use bitwise and it is running in the same amount of time. (100,000 iterations in about 6 milliseconds)
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.
it was just for completeness, certainly not to show off, however list comprehensions are virtually identical to python and much more comprehensible than the line noise introduced in ruby list comprehensions.
the fact that this is called out twice as being "ugly" twice in comparison to the other examples auggests to me that erlang is called out as being ugly purely from hearsay as opposed to actually being ugly.
(I do think erlang can be ugly, I also think thats its biggest strength, but I dont think this example is ugly)
erlang is called out as being ugly purely from hearsay
I have no preconceptions about Erlang. I've never seen the opinion that Erlang is ugly, and your code above is the first Erlang I've ever seen. I also know nothing about Haskell, but I found the Haskell example to be the most elegant.
So, a more reasonable conclusion: The fact that two people found the code ugly suggests that it is ugly.
public class filter {
public static void main (String [] args) {
System.out.println("2, 4");
}
}
FTFY, While it may be fun that show off with your favorite language, the Java version is intentionally complex.
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
The other examples filter from an existing list, essentially used as a placeholder. It could be substituted with any other list and the code sample would work. Your example use some hardcoded values - strings at that, not integers - hence not the same thing at all.
The other ones are meant to be run in a REPL, but java doesn't have one (lest you count beanshell, which ships with a really old version of java, one before generics) so I had to print it explicitly
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.