Groovy programming language
From Free net encyclopedia
Template:Wiktionarypar Groovy is an object-oriented programming language designed for the Java platform as an alternative to the Java programming language with features from Python, Ruby and Smalltalk.
Groovy uses a Java-like syntax which is dynamically compiled to JVM bytecodes and that works seamlessly with other Java code and libraries. The Groovy compiler can be used as an alternative to javac 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 through the Java Community Process under JSR 241.
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
Contents |
[edit]
Examples
class Foo { def doSomething() { def data = ["name": "James", "location": "London"] for (e in data) { println("entry ${e.key} is ${e.value}") } } def closureExample(collection) { collection.each { println("value ${it}") } } static void main(args) { def values = [1, 2, 3, "abc"] def foo = new Foo() foo.closureExample(values) foo.doSomething() } }
[edit]
Comparison between Java code and comparable Groovy code
[edit]
Java
class Filter { public static void main(String[] args) { List<String> list = Arrays.asList("Rod", "James", "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); } } }
[edit]
Groovy
list = ["Rod", "James", "Chris"] shorts = list.findAll { it.size() <= 4 } shorts.each { println it }
[edit]