Groovy (programming language)
From Wikipedia, the free encyclopedia
Paradigm: | object-oriented, scripting |
---|---|
Appeared in: | 2003 |
Designed by: | JCP |
Typing discipline: | static, strong |
Influenced by: | Python, Ruby, Perl, Smalltalk, Java |
Groovy is an object-oriented programming language for the Java Platform as an alternative to the Java programming language. It can be viewed as a scripting language for the Java Platform, as it has features similar to those of Python, Ruby, Perl, and Smalltalk. In some contexts, the name JSR 241 is used as an alternate identifier for the Groovy language.
Groovy uses a Java-like curly bracket syntax which is dynamically compiled to JVM bytecodes and that works seamlessly with other Java code and libraries. The Groovy compiler can be used to generate standard Java bytecode to be used by any Java project or it can be used dynamically as a scripting language.
Groovy is currently undergoing standardization via the Java Community Process under JSR 241. Groovy 1.0 was released on January 2, 2007.
Contents |
[edit] Language Features
Groovy has a number of features not found in standard Java:
- Static typing and dynamic typing
- Native syntax for lists, maps, arrays, and regular expressions
- Closures
- Operator overloading
[edit] Syntax Comparison
The following presents a representative side-by-side comparison of Java with Groovy:
Standard Java (Java 5+)
class Filter { public static void main(String[] args) { List<String> list = Arrays.asList("Rod", "Carlos", "Chris"); List<String> shorts = new ArrayList<String>(); for (String item : list) { if (item.length() <= 4) { shorts.add(item); } } for (String item : shorts) { System.out.println(item); } } }
Groovy
list = ["Rod", "Carlos", "Chris"] shorts = list.findAll { it.size() <= 4 } shorts.each { println it }
[edit] Markup Language Support
One noteworthy feature of Groovy is its native support for various markup languages such as XML and HTML. This feature enables the definition and manipulation of many types of heterogeneous data assets with a uniform syntax and programming methodology. For example:
the following Groovy code ...
import groovy.xml.MarkupBuilder def myXMLDoc = new MarkupBuilder() myXMLDoc.workbook { worksheet(caption:"Employees") { row(fname:"John", lname:"McDoe") row(fname:"Nancy", lname:"Davolio") } worksheet(caption:"Products") { row(name:"Veeblefeetzer", id:"sku34510") row(name:"Prune Unit Zappa", id:"sku3a550") } } println myXMLDoc
... produces the XML result:
<workbook> <worksheet caption='Employees'> <row fname="John" lname="McDoe" /> <row fname="Nancy" lname="Davolio" /> </worksheet> <worksheet caption='Products'> <row name="Veeblefeetzer" id="sku34510" /> <row name="Prune Unit Zappa" id="sku3a550" /> </worksheet> </workbook>
[edit] History
James Strachan first talked about the development of Groovy in his blog in August 2003. Several versions were released between 2004 and 2006. After the JCP standardization process began, the version numbering was changed and a version called "1.0" was released on Tuesday, January 2, 2007.
[edit] References
- Dierk Koenig with Andrew Glover, Paul King, Guillaume Laforge and Jon Skeet. Groovy in Action . Manning, 2006. ISBN 1-932394-84-2.
[edit] See also
- Boo - a language with similar objectives but targeting the Common Language Infrastructure
- JRuby - Pure Java reimplementation of Ruby
- Jython - A Java implementation of Python
- BeanShell - A lightweight interpreter of Java source codes