Thursday, December 26, 2013

What benefits does Groovy offer over the use of Java other than more complicated syntax?
share|improve this question
 
I doubt anyone would consider more complicated syntax a benefit. The syntax of Groovy is more complicated than that of Java because it is a more powerful language. Whether or not Groovy is preferable to Java largely depends on where you position yourself on the simplicity-power continuum. –  Don Mar 24 '11 at 16:41 
9 
I wish we have some community where we can migrate such good (but not constructive !) closed questions and keep them active –  hop Mar 28 at 18:45
add comment

closed as not constructive by Bo PerssonLingerRalfPopsJean-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.

11 Answers

up vote38down voteaccepted
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".
for (file in args) {
        new File(file).eachLine { line -> 
                if (line =~ /new/) {
                        println line
                }
        }
}
Note, that is the entire program, not a snippet from a larger program.
share|improve this answer
3 
It's interesting that Groovy gets used, when JavaScript comes with the JDK and works just as well.java.sun.com/javase/6/docs/technotes/tools/share/jsdocs/… –  Steven Huwig Oct 27 '09 at 1:26
12 
@Steven - calling between Java and Groovy is a lot more straightforward than Java and JS, so I don't agree that they both "work just as well" as each other on the JVM –  Don Nov 18 '09 at 4:20
 
@Steven Huwig Is there much overhead between dynamic and static typing? Seems to be the horn most c programmers blow when they talk about speed –  OVERTONE Aug 10 '11 at 15:15 
 
@OVERTONE I still think Groovy is a bad choice for people regardless of static or dynamic typing preference. Maybe you meant to ask Chas. Owens? –  Steven Huwig Aug 11 '11 at 15:19
1 
@StevenHuwig The direct follow-up to that oft-cited post kinda nullifies your point: "I still like Groovy/Ruby/JavaScript etc" –  OverZealous Sep 20 '11 at 6:15
show 12 more comments
The Groovy language has a wide range of features that are sadly lacking in Java. These include:
  • Properties
  • Closures
  • Metaprogramming
  • Multi-line strings
  • String interpolation
  • Mixins/Categories
  • Named arguments
  • Default arguments
  • Collection literals
  • Operator overloading
  • GPath expressions
  • Additional operators, e.g. '?.' (a null-safe version of Java's '.' operator)
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.
share|improve this answer
 
This link to stackexchange provides other reasons programmers.stackexchange.com/questions/40420/why-groovyjava –  Barun Nov 1 at 7:28 
add comment
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
share|improve this answer
1 
+1 for both. XMLSlurper makes reading and writing XML so simple. –  Damo Jul 4 '11 at 5:52
1 
For my projects, working with XML is reason by itself to use Groovy. Groovy is one jar file and for that you get very easy time going to/from XML. Code is as simple as x.y.z (after slurping)! –  Mark Jul 25 '11 at 13:57
add comment
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.
share|improve this answer
add comment
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
share|improve this answer
16 
strictly speaking "it lets you use your existing java codebase" is not an advantage that Groovy has over Java ;-) –  Joachim Sauer Oct 1 '09 at 18:44
3 
I reckon it is, unless java also lets you use your existing groovy codebase... ;) –  Alex Mar 1 '10 at 16:49
6 
Java lets you use your existing Java codebase. –  Seun Osewa Jun 5 '10 at 19:44
1 
@Alex Groovy let's Java use your existing Groovy codebase. :) –  madth3 Nov 17 '12 at 3:35
add comment
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
share|improve this answer
4 
yeah I feel uneasy about not knowing exactly what that type is –  Kim Jong Woo Jul 11 '11 at 1:49
 
You say "strong typing" but mean "static typing". Java and Groovy are both strong typed. –  Alex Jul 11 '12 at 20:45
 
"I use the IDE to write most of my code" -- you know this really scares some people, right? :-) –  RoboprogDec 8 '12 at 5:09
 
The functions, er, methods, should be short enough that it's obvious what is in each variable. If there are some constraints on the parameters, such as methods that should be available, perhaps it should be documented in the header, unless an actual type is given for the parameter. –  Roboprog Dec 8 '12 at 5:10
1 
You may end up writing as much / more code just because of all of the type-checking code you need to add to the code in a dynamic language .... or ... dont add the type checking code and just have your app croak when it gets a string when it should be given a float. –  Michael.M Jun 17 at 23:46
show 3 more comments
Everything that Chas. Owens said in his accepted answer at the top really hits the nail on the head:
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.
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.
share|improve this answer
7 
This is soooo subjective. Your logic also suggests that Perl is as easy as Groovy and easier than Python which is, well, just ridiculous. TCL easier than PHP or even Java - that's a LOL. All these stats show nothing more than personal prefenrences of their coders. –  Nikita Volkov Oct 23 '11 at 13:16
1 
These percentages are not completely accurate. The PLEAC for groovy contains a lot of comments rather than code. For example, the last 10 cookbook entries in the groovy file handling section contain comments explaining that such things are doable using straight Java, a groovy implementation is missing.pleac.sourceforge.net/pleac_groovy/fileaccess.html –  buzz3791 Apr 25 '12 at 16:23
add comment
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.
share|improve this answer
add comment
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
share|improve this answer
add comment
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:
List booksILike = new ArrayList();
becomes this in Groovy:
def booksILike = []
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.
share|improve this answer
add comment
New features added to Groovy not available in Java.
1.closures
2.native syntax for lists and maps
3.GroovyMarkup and GPath support
4.native support for regular expressions
5.polymorphic iteration and powerful switch statement
dynamic and static typing is supported - so you can omit the type declarations on methods, fields and variables
6.you can embed expressions inside strings
7.lots of new helper methods added to the JDK
8.simpler syntax for writing beans for both properties and adding event listeners
9.safe navigation using the ?. operator, e.g. "variable?.field" and "variable?.method()" - no more nested ifs to check for null clogging up your code
share|improve this answer
add comment

No comments: