Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is Groovy and in which cases we need Groovy?

 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
Matthew Taylor
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an issue with groovy that sometimes it does not spit proper exception messages which one would normally see when running Java programs.
 
Matthew Taylor
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.5.6
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic