What’s Groovy?
If you know about Groovy already, you may skip this introduction.
For those of you who are not aware of it yet, it’s one of the most popular Dynamic language for the Java platform (JVM). 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. It can be used as a scripting language for the Java Platform.
Interesting? You wanna read more? If yes, do visit this page – http://groovy.codehaus.org/
Okay, no more boring theories. Here’s the step-by-step guide to get you started with Groovy programming.
1. Install Groovy-Eclipse Plugin
If you’re a Java developer, it’s most likely that you have Eclipse IDE in your machine. Install Groovy-Eclipse plugin on top of that. The Groovy-Eclipse Plugin provides Eclipse-based tooling support for the Groovy programming language. Groovy-Eclipse allows you to edit, compile, run, and debug Groovy scripts and classes from the Eclipse SDK.
2. Create a new Groovy project
Give a name for the project and choose a JRE version.
3. Create a new Groovy class
Give it a name. Let’s say it’s GroovyClass1.groovy and include a main method.
//A Groovy Class class GroovyClass1 { static void main(def args){ //Groovy code } }
4. Print a message – “Hello Groovy!”
//A Groovy Class class GroovyClass1 { static void main(def args){ println "Hello Groovy!" } }
Run it as Groovy Script and you’ll see the output in console.
5. Add some more stuff you learn into main method and try running the program
//A Groovy Class class GroovyClass1 { static void main(def args){ // Print a HELLO message def language = "Groovy" println "Hello $language!" // List def gList = ["C", "C++", "Java"] gList.add("Groovy") System.out.println("Iterating through and displaying items in Groovy List..."); gList.each{ println it} // Map def scores = [ "Sachin": 100, "Dravid": 75, "Sehwag": "Did Not Bat"] scores.put("Yuvaraj", 45) // Display all key-value pairs scores.each{ println it} // Display Sachin's score println "Sachin's score: " + scores["Sachin"] } }
Related articles
- Getting started with Python programming – A simple example with all basic stuff (singztechmusings.wordpress.com)