44
21
| |||||||||
|
closed as not constructive by Bo Persson, Linger, Ralf, Pops♦, Jean-François Corbett Nov 18 '12 at 18:51
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.
38
|
Dynamic typing. For people coming from Perl, Python, Ruby, etc. Java's type system is a straitjacket that serves no purpose but to get in the way. Other nice things are regexes as first class citizens and closures. For instance, how much more code do you need in Java to read in every file passed on the commandline and print out only the lines that contain the string
"new" .
Note, that is the entire program, not a snippet from a larger program.
| ||||||||||||||||||||
|
28
|
The Groovy language has a wide range of features that are sadly lacking in Java. These include:
And lots more that I can't think of at the moment. The net result is that to accomplish a given task in Groovy generally takes a lot less code than in Java. Much of the code that you don't have to write when using Groovy could be considered 'boilerplate'.
It's not only the extra language features offered by Groovy, it's also the additional methods Groovy addsto the most commonly used JDK classes. These enable one to make the most of Groovy's language features (closures, in particular) when working with Java library classes.
The dynamic nature of Groovy also reduces the amount of code, though the advantages/disadvantages of static and dynamic typing is a debate for another day.
| ||||
|
20
|
Groovy has nice improvements over Java when iterating over Collections. It also provides closures which can be convenient, and enables some pretty sweet stuff for working with XML with its XmlSlurper
| ||||||||
|
19
|
Although this question is kinda old, I feel I have an important one to add:
Groovy is a nice alternative to shell scripting for developers with a Java background.
I think most answers above outlined why you would choose Groovy over Java where you would have used Java. I think it's important to add where you would choose Groovy over Java where Java wouldn't have ever come into consideration. For instance, just recently I used Groovy to read a CSV file, access a REST service and write a new CSV file (took less than 100 lines of Groovy code). Additionally, Groovy's Grapes makes it extremely easy to leverage the power of libraries you're used to from day to day programming (for my example, I used net.sourceforge.javacsv:javacsv) for this kind of run-once scripts.
| ||
add comment |
16
|
Groovy syntax is often simpler than in java, check for example Groovy Beans and closures, here
Besides, it lets you use your existing java codebase
| ||||||||||||||||
|
7
|
I find the argument that "Groovy takes less lines" to do something a little misleading as when I code Java - I use the IDE to write most of my code. The strong typing also let's me know exactly what I can do with a variable instead of guessing when all variables have been scoped by "def". My experience is from a background of editing existing Groovy Code
| ||||||||||||||||||||
|
7
|
Everything that Chas. Owens said in his accepted answer at the top really hits the nail on the head:
If you only really know Java and nothing else — something I find both mysteriously and distressingly common — then you just don’t know what you’re missing out on.
One highly intriguing comparison site is PLEAC, the “Programming Language Examples Alike Cookbook”. Their site gives you equivalent code examples implemented in various programming languages for a whole bunch of diverse but surely familiar and useful tasks. Here along with the percentage completed are just a few of the languages that they’ve implemented all these in:
The percentage done is perhaps some reflection of how hard it is coding stuff up each language. Sure, Java is easier than C, but it is still not even half as easy as Groovy. It’s a whole different world.
If you look those example solutions, you will see why Groovy is way up there at the top: because Groovy gets out of the way and lets you get your job without (a lot of) Java’s hassle and fuss.
Give those comparative soluitions glance. And do give Groovy a glance, too — who knows, you might even have fun again programming in Groovy. It’s at least worth a shot.
| ||||||||
|
5
|
The metaclass concept is nice too, it allows you to augment existing classes with new methods, dynamically.
However, I'm given to understand that the JSR-292 work will generalise this ability to all languages on the JVM via interface injection.
| |||
add comment |
4
|
The important difference between Java and Groovy is that Groovy adds dynamic language capabilities such as closures, dynamic typing, mixins etc.. found mostly in dynamic languages such as Python, Ruby. Scala is another language on the JVM which combines the benefits of both statically and dynamically typed languages. For more information between the difference between these two JVM languages, seehttp://stackoverflow.com/questions/711913/what-are-the-key-differences-between-scala-and-groovy
| ||
add comment |
4
|
Taking a step back from the other answers: it takes less code to get the same amount of work done.
For example, Groovy has a mess syntactic sugar to make lists and maps act like first-class citizens of the language. Since it's all Java bytecode under the hood, it just uses the existing Java collections, but this line in Java:
becomes this in Groovy:
Literally, those do exactly the same thing. However, the groovy one is just faster to type and less characters equals less things to get wrong. The whole language is kind of built on that philosophy: use as few words as possible to get the job done. Groovy's black magic hides away all the baroque boilerplate that Java needs to work and lets you get on with actually writing the code. It's awesome.
And, since Groovy does all that grunt work for you, it's (almost) always right. The amount of bugs that were actually in the Java boilerplate code kinda blew my mind: writing in Groovy tends to just work.
Going back to Java after working in Groovy was like slowing to a crawl.
| ||
add comment |
4
|
New features added to Groovy not available in Java.
| ||
add comment |