Groovy: Basic Introduction

Published Feb 22, 2017
Groovy: Basic Introduction

Overview of Groovy

  • Groovy is like a super version of Java but much easier to understand and use than Java
  • You can mix Groovy and Java objects together
    (e.g. Groovy class extending java class implementing groovy interface and vice versa)
  • It can leverage Java's existing libraries and its enterprise capabilities
  • It has cool productivity features like closures, builders, and dynamic typing

Sample class in both Java and Groovy:

Greet.groovy (Greet class in groovy language)

class Greet {
  def name

  Greet(who) {
    name = who[0].toUpperCase() + who[1..-1] 
  }
  
  def salute() { 
      println "Hello $name!" 
   }
}

g = new Greet('world')  // create object
g.salute()  

Greet.class (Greet class in Java language)

public class Greet {
  private String name;

  public Greet(String name) {
    this.name = name.getCharAt(0).toUpperCase() + name.subString(1); 
  }
  
  public void salute() { 
      System.out.println("Hello " + name);
   }

   public String getName() {
     return this.name;
   }

   public void setString(String name) {
     this.name = name;
   }
}

If you'd like to schedule a time to chat on Codementor, please click here to schedule a chat with me!

Discover and read more posts from Yatendra Goel
get started
Enjoy this post?

Leave a like and comment for Yatendra

1
Be the first to share your opinion

Get curated posts in your inbox

Read more posts to become a better developer