Groovy programming language
Encyclopedia
Groovy is an object-oriented
programming language
for the Java platform. It is a dynamic language
with features similar to those of Python
, Ruby
, Perl
, and Smalltalk
. It can be used as a scripting language
for the Java Platform.
Groovy uses a Java-like bracket syntax. It is dynamically compiled to Java Virtual Machine
(JVM) bytecode
and interoperates with other Java code and libraries
. Most Java code is also syntactically valid Groovy.
Groovy 1.0 was released on January 2, 2007.
After various betas and release candidates numbered 1.1, on December 7, 2007, Groovy 1.1 Final was released and immediately rebranded as Groovy 1.5 as a reflection of the many changes that were made.
In July 2009, Strachan wrote on his blog that "I can honestly say if someone had shown me the Programming in Scala book by Martin Odersky
, Lex Spoon & Bill Venners back in 2003 I'd probably have never created Groovy." Strachan left the project silently a year before the Groovy 1.0 release in 2007.
s.
Groovy features not available in Java include both static and dynamic typing (with the def keyword), closures
, operator overloading
, native syntax for lists and associative array
s (maps), native support for regular expression
s, polymorphic iteration, expressions embedded inside strings, additional helper methods, and the safe navigation operator "?." to automatically check for nulls (for example, "variable?.method", or "variable?.field").
Groovy's syntax can be made far more compact than Java. For example, a declaration in Standard Java 5+ such as:
can be expressed in Groovy as:
Groovy provides native support for various markup language
s such as XML
and HTML
, accomplished via an inline DOM
syntax. This feature enables the definition and manipulation of many types of heterogeneous data assets with a uniform and concise syntax and programming methodology.
Unlike Java, a Groovy source code file can be executed as an (uncompiled) script
if it contains code outside any class definition, is a class with a main method, or is a Runnable or GroovyTestCase. But, unlike some script languages such as Bourne Shell, a Groovy script is fully parsed, compiled, and generated before execution (similar to Perl and Ruby). (This occurs under the hood, and the compiled version is not saved as an artifact of the process.)
GroovyBeans are Groovy's version of JavaBeans
. Groovy implicitly generates accessor and mutator methods. In the following code, setColor(String color) and getColor are implicitly generated; and the last two lines, which appear to access color directly, are actually calling the implicitly generated methods.
Groovy offers simple, consistent syntax for handling lists and maps, reminiscent of Java's array syntax.
s support Groovy:
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...
programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....
for the Java platform. It is a dynamic language
Dynamic programming language
Dynamic programming language is a term used broadly in computer science to describe a class of high-level programming languages that execute at runtime many common behaviors that other languages might perform during compilation, if at all...
with features similar to those of Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...
, Ruby
Ruby (programming language)
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...
, Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...
, and Smalltalk
Smalltalk
Smalltalk is an object-oriented, dynamically typed, reflective programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human–computer symbiosis." It was designed and created in part for educational use, more so for constructionist...
. It can be used as a scripting language
Scripting language
A scripting language, script language, or extension language is a programming language that allows control of one or more applications. "Scripts" are distinct from the core code of the application, as they are usually written in a different language and are often created or at least modified by the...
for the Java Platform.
Groovy uses a Java-like bracket syntax. It is dynamically compiled to Java Virtual Machine
Java Virtual Machine
A Java virtual machine is a virtual machine capable of executing Java bytecode. It is the code execution component of the Java software platform. Sun Microsystems stated that there are over 4.5 billion JVM-enabled devices.-Overview:...
(JVM) bytecode
Bytecode
Bytecode, also known as p-code , is a term which has been used to denote various forms of instruction sets designed for efficient execution by a software interpreter as well as being suitable for further compilation into machine code...
and interoperates with other Java code and libraries
Library (computer science)
In computer science, a library is a collection of resources used to develop software. These may include pre-written code and subroutines, classes, values or type specifications....
. Most Java code is also syntactically valid Groovy.
Groovy 1.0 was released on January 2, 2007.
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 January 2, 2007.After various betas and release candidates numbered 1.1, on December 7, 2007, Groovy 1.1 Final was released and immediately rebranded as Groovy 1.5 as a reflection of the many changes that were made.
In July 2009, Strachan wrote on his blog that "I can honestly say if someone had shown me the Programming in Scala book by Martin Odersky
Martin Odersky
Martin Odersky is a German computer scientist and professor of programming methods at the EPFL. He specialises in code analysis and programming languages.In 1989 Odersky received his Ph.D...
, Lex Spoon & Bill Venners back in 2003 I'd probably have never created Groovy." Strachan left the project silently a year before the Groovy 1.0 release in 2007.
Features
Many (but not all) valid Java files are also valid Groovy files. Although the two languages are similar, Groovy code can be more compact, because it does not require all the elements that Java requires. This makes it possible for Java programmers to gradually learn Groovy by starting with familiar Java syntax before acquiring more Groovy idiomProgramming idiom
A programming idiom is a means of expressing a recurring construct in one or more programming languages. Generally speaking, a programming idiom is an expression of a simple task or algorithm that is not a built-in feature in the programming language being used, or, conversely, the use of an...
s.
Groovy features not available in Java include both static and dynamic typing (with the def keyword), closures
Closure (computer science)
In computer science, a closure is a function together with a referencing environment for the non-local variables of that function. A closure allows a function to access variables outside its typical scope. Such a function is said to be "closed over" its free variables...
, operator overloading
Operator overloading
In object oriented computer programming, operator overloading—less commonly known as operator ad-hoc polymorphism—is a specific case of polymorphism, where different operators have different implementations depending on their arguments...
, native syntax for lists and associative array
Associative array
In computer science, an associative array is an abstract data type composed of a collection of pairs, such that each possible key appears at most once in the collection....
s (maps), native support for regular expression
Regular expression
In computing, a regular expression provides a concise and flexible means for "matching" strings of text, such as particular characters, words, or patterns of characters. Abbreviations for "regular expression" include "regex" and "regexp"...
s, polymorphic iteration, expressions embedded inside strings, additional helper methods, and the safe navigation operator "?." to automatically check for nulls (for example, "variable?.method", or "variable?.field").
Groovy's syntax can be made far more compact than Java. For example, a declaration in Standard Java 5+ such as:
can be expressed in Groovy as:
Groovy provides native support for various markup language
Markup language
A markup language is a modern system for annotating a text in a way that is syntactically distinguishable from that text. The idea and terminology evolved from the "marking up" of manuscripts, i.e. the revision instructions by editors, traditionally written with a blue pencil on authors' manuscripts...
s such as XML
XML
Extensible Markup Language is a set of rules for encoding documents in machine-readable form. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all gratis open standards....
and HTML
HTML
HyperText Markup Language is the predominant markup language for web pages. HTML elements are the basic building-blocks of webpages....
, accomplished via an inline DOM
Document Object Model
The Document Object Model is a cross-platform and language-independent convention for representing and interacting with objects in HTML, XHTML and XML documents. Aspects of the DOM may be addressed and manipulated within the syntax of the programming language in use...
syntax. This feature enables the definition and manipulation of many types of heterogeneous data assets with a uniform and concise syntax and programming methodology.
Unlike Java, a Groovy source code file can be executed as an (uncompiled) script
Scripting language
A scripting language, script language, or extension language is a programming language that allows control of one or more applications. "Scripts" are distinct from the core code of the application, as they are usually written in a different language and are often created or at least modified by the...
if it contains code outside any class definition, is a class with a main method, or is a Runnable or GroovyTestCase. But, unlike some script languages such as Bourne Shell, a Groovy script is fully parsed, compiled, and generated before execution (similar to Perl and Ruby). (This occurs under the hood, and the compiled version is not saved as an artifact of the process.)
GroovyBeans are Groovy's version of JavaBeans
JavaBeans
JavaBeans are reusable software components for Java. Practically, they are classes written in the Java programming language conforming to a particular convention. They are used to encapsulate many objects into a single object , so that they can be passed around as a single bean object instead of as...
. Groovy implicitly generates accessor and mutator methods. In the following code, setColor(String color) and getColor are implicitly generated; and the last two lines, which appear to access color directly, are actually calling the implicitly generated methods.
Groovy offers simple, consistent syntax for handling lists and maps, reminiscent of Java's array syntax.
IDE support
Many integrated development environmentIntegrated development environment
An integrated development environment is a software application that provides comprehensive facilities to computer programmers for software development...
s support Groovy:
- EclipseEclipse (software)Eclipse is a multi-language software development environment comprising an integrated development environment and an extensible plug-in system...
, through Groovy-Eclipse - IntelliJ IDEAIntelliJ IDEAIntelliJ IDEA is a commercial Java IDE by JetBrains. It is often simply referred to as "IDEA" or "IntelliJ."-History:The first version of IntelliJ IDEA was released in January 2001, and at the time was the only available Java IDE with advanced code navigation and code refactoring capabilities...
, through the Jet Groovy Plugin - NetBeansNetBeansNetBeans refers to both a platform framework for Java desktop applications, and an integrated development environment for developing with Java, JavaScript, PHP, Python, Groovy, C, C++, Scala, Clojure, and others...
, since version 6.5 - TextMateTextMateTextMate is a general-purpose GUI text editor for Mac OS X created by Allan Odgaard. Popular with programmers, some notable features include declarative customizations, tabs for open documents, recordable macros, folding sections and snippets, shell integration, and an extensible bundle...
- JDeveloperJDeveloperJDeveloper is a freeware IDE supplied by Oracle Corporation. It offers features for development in Java, XML, SQL and PL/SQL, HTML, JavaScript, BPEL and PHP...
, for use with Oracle ADF - Xappworks, a cloud-based IDE currently in beta & scheduled to enter the market early 2011
See also
- Comparison of programming languagesComparison of programming languagesProgramming languages are used for controlling the behavior of a machine . Like natural languages, programming languages conform to rules for syntax and semantics.There are thousands of programming languages and new ones are created every year...
- Groovy++
- BeanShellBeanShellBeanShell is a Java scripting language, invented by Patrick Niemeyer. It runs in the Java Runtime Environment and uses Java syntax, in addition to scripting commands and syntax.- Features :...
- ClojureClojureClojure |closure]]") is a recent dialect of the Lisp programming language created by Rich Hickey. It is a general-purpose language supporting interactive development that encourages a functional programming style, and simplifies multithreaded programming....
- Scala
- Grails (framework)Grails (Framework)Grails is an open source web application framework which uses the Groovy programming language . It is intended to be a high-productivity framework by following the "coding by convention" paradigm, providing a stand-alone development environment and hiding much of the configuration detail from the...
- Griffon (framework)Griffon (framework)Griffon is an open source Rich Client Platform framework which uses the Groovy programming language . Griffon is intended to be a high-productivity framework by rewarding use of the Model-View-Controller paradigm, providing a stand-alone development environment and hiding much of the configuration...
- Ruby
- JRubyJRubyJRuby is a Java implementation of the Ruby programming language, being developed by the JRuby team. It is free software released under a three-way CPL/GPL/LGPL license...
- JythonJythonJython, successor of JPython, is an implementation of the Python programming language written in Java.-Overview:Jython programs can seamlessly import and use any Java class. Except for some standard modules, Jython programs use Java classes instead of Python modules...
- Boo
- PnutsPnutsPnuts is a dynamic scripting language for the Java platform. It is designed to be used in a dual language system with the Java programming language. The goals of the Pnuts project are to provide a small, fast scripting language that has tight integration with the Java language...
- ZK Framework
- Project ZeroProject ZeroWebSphere sMash is a development and runtime environment from IBM for the creation of dynamic web applications using the scripting languages Groovy and PHP...
- Tellurium (software)Tellurium (software)The Tellurium Automated Testing Framework is a UI module-based web automated testing framework. is a portable software testing framework for web applications that runs on top of Selenium. Tellurium tests are written in Groovy and pure DSL scripts. Tellurium supports both TestNG and JUnit unit...
External links
- JSR 241
- An introduction to Groovy
- Groovy for the Office
- Groovy Zone - DZone Groovy news aggregator
- About Groovy - About Groovy & news
- Making Java Groovy by Dr. Ken Kousen
- Certified Groovy training from SpringSource University
- Groovy articles directory
- Interview with Guillaume Laforge about Groovy, from QCon London 2007
- Interview with Guillaume Laforge and Graeme Rocher about Groovy and Grails, from SpringOne New Orleans 2009