File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Groovy and the fly likes What is Groovy and in which cases we need Groovy? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Languages » Groovy
Bookmark "What is Groovy and in which cases we need Groovy?" Watch "What is Groovy and in which cases we need Groovy?" New topic
Author

What is Groovy and in which cases we need Groovy?

Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

I am totally new to Groovy and just want to know that what is Groovy and in which cases we use them? Are there any special advantages to use Groovy?


My Blog SCJP 5 SCWCD 5
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Most of what you are asking is answered on the Groovy home page.

You can already do just about anything in Java. With Groovy, you can still do those same things, but often with far less code.

For instance, a friend of mine was asked in an interview to use Java to find the intersection of two lists. This requires some thought using Java but I was able to solve and test this problem in a matter of seconds using Groovy:

Result: [1, 3, 5, 7]


A good workman is known by his tools.
Matthew Taylor
Rancher

Joined: Jun 13, 2004
Posts: 110
Originally posted by Marc Peabody:
For instance, a friend of mine was asked in an interview to use Java to find the intersection of two lists. This requires some thought using Java but I was able to solve and test this problem in a matter of seconds using Groovy:

Result: [1, 3, 5, 7]


Or....


It is a few more characters of code, but it does speak for itself .


Grails Consultant
http://dangertree.net
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Great reply man.

I want to know that can groovy be used in the java classes itself or it is something like ANT. Is it integrable with java code. Can we use Groovy in between java code in methods?
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Originally posted by Himanshu Gupta:
I want to know that can groovy be used in the java classes itself or it is something like ANT. Is it integrable with java code. Can we use Groovy in between java code in methods?

You can create Groovy classes much like you can create Java classes. Groovy and Java both get compiled down to JVM bytecode, so you can call Groovy classes from your Java classes and vice versa.

On mixing Java and Groovy syntax in the same class file: you cannot put Groovy style code in the middle of a Java class. You can, however, put nearly any Java style code in the middle of a Groovy class.

A lot of the benefits of Groovy syntax are similar to what you can get from other JVM languages like JRuby.

I recommend downloading Groovy and playing around with the GroovyConsole using a few small tutorials. If you're already familiar with Swing in Java, you might especially have fun following some SwingBuilder Groovy tutorials.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

I have an issue with groovy that sometimes it does not spit proper exception messages which one would normally see when running Java programs.


Groovy
Matthew Taylor
Rancher

Joined: Jun 13, 2004
Posts: 110
Originally posted by Prad Dip:
I have an issue with groovy that sometimes it does not spit proper exception messages which one would normally see when running Java programs.


That is a common complaint among Java programmers migrating to Groovy. For the short term, you'll just have to get used to it . I know the Groovy devs have this issue in their sites, and would like to provide more succinct stacktraces.
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Originally posted by Prad Dip:
I have an issue with groovy that sometimes it does not spit proper exception messages which one would normally see when running Java programs.

Which version of Groovy are you using? There's an exception sanitizer in the Groovy shell that I believe was put there not too incredibly long ago. I think the sanitizer was borrowed from the Grails project and really cleaned up a lot starting in Groovy 1.6.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

1.5.6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is Groovy and in which cases we need Groovy?