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 idioms.
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 arrays (maps), native support for regular expressions, 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").
Since version 2 Groovy also supports modularity, being able to ship only the needed jars according to the project needs, thus reducing the size of groovy's lib, type checking, static compilation, Project Coin syntax enhancements, multicatch blocks and ongoing performance enhancements using JDK7's invoke dynamic instruction.
Groovy's syntax can be made far more compact than Java. For example, a declaration in Standard Java 5+ such as:
for (String it : new String {"Rod", "Carlos", "Chris"}) if (it.length <= 4) System.out.println(it);can be expressed in Groovy as:
.findAll{it.size <= 4}.each{println it}Groovy provides native support for various markup languages 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, 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. 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.)
Read more about this topic: Groovy (programming Language)
Famous quotes containing the word features:
“Art is the child of Nature; yes,
Her darling child, in whom we trace
The features of the mothers face,
Her aspect and her attitude.”
—Henry Wadsworth Longfellow (18071882)
“It is a tribute to the peculiar horror of contemporary life that it makes the worst features of earlier timesthe stupefaction of the masses, the obsessed and driven lives of the bourgeoisieseem attractive by comparison.”
—Christopher Lasch (b. 1932)
“However much we may differ in the choice of the measures which should guide the administration of the government, there can be but little doubt in the minds of those who are really friendly to the republican features of our system that one of its most important securities consists in the separation of the legislative and executive powers at the same time that each is acknowledged to be supreme, in the will of the people constitutionally expressed.”
—Andrew Jackson (17671845)