Groovify your Java Code
A guide to Groovy syntax for Java coders


By Hervé Vũ Roussel for Agile Vietnam Coding Dojo 2014

Interactive slides at: https://slides.com/hroussel/groovify-your-java-code

Print / bracket, semicol optional

System.out.println("Hello World!");
println "Hello World!"

Return optional

String getCurrentCity() {
  return "Ho Chi Minh City";
}
def getCurrentCity() {
  "Ho Chi Minh City"
}

Try/catch optional

try{
  Reader reader = new FileReader("/vietnam-cities.txt")
}
catch(FileNotFoundException e) {
  e.printStackTrace()
}
def reader = new FileReader("/vietnam-cities.txt")

Duck typing

String s = "Hello"; 
String c = 'c';
Integer i = new Integer(1);
BigDecimal d = new BigDecimal(1.2);
def s = "Hello"
def c = 'c'
def i = 1
def d = 1.2

Strong typing

String s = "Hello"; 
Character = new Character(‘c’);
Integer i = new Integer(1);
Float d = new Float(1.2);
String s = "Hello"
Character c = 'c'
Integer i = 1
Float f = 1.2

Existential operator (Elvis)

if (city != null) {
  if (city.getAirport() != null) {
    city.getAirport().getCode();
  }
}
city?.getAirport()?.getCode()

Truth

if (1 != 0)
if (new City() != null) 
if ("John".equals(""))
if (["HCMC", "Hanoi"].length > 0)
if (1)
if (city)
if ("John")
if (["HCMC", "Hanoi"])

Collections

String [] cities = ["HCMC","Hanoi"];
System.out.println(cities[0]);

Map<String,String> airports = new HashMap<String,String>();
airports.put("SGN", "Ho Chi Minh City");
airports.put("CDG", "Paris");
System.out.println(airports.get("SGN"));
def cities = ["HCMC","Hanoi"]
println cities[0]

def airports = [SGN:"Ho Chi Minh City", CDG:"Paris"]
println airports.SGN

Ranges

List<String> alphabet = new ArrayList<String>();
for(int i=0;i<26;i++) {
  alphabet.add(Character.toChars(i+97));
}
def alphabet = "a".."z"
​

GString

System.out.println("Hello " + user.name);
println "Hello ${user.name}"

Groovy Scripts

public class Script1 {
  public static void main(String[] args){
    String greeting = "Hello " + args[0];
    System.out.println(greeting);
  }
}
greeting = "Hello ${args[0]}"
println greeting

POGOs

public class City {
  private String name;

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

System.out.println(new City("HCMC").getName());
class City {
  String name
}

println new City(name: "HCMC").name

Compare objects

a.equals(b);
a.compareTo(b);
a == b
a < b

Default parameters

public class Trip {
  public Trip(String source, String dest, String type) {
    if (type == null)
      this.type = "Car"; 
  }
}

new Trip("HCMC", "Vung Tau", null);
class Trip {
  Trip(String source, String dest, String type = "Car") {
    this.type = type;
  }
}

new Trip("HCMC", "Vung Tau")

Closures

List result = new ArrayList();
for(city in city) {
  if (city.startsWith("H")
    result.add(city)
}
cities.findAll { it.startsWith("H") }

Closures (cont.)

​class City {  
  String name
}

def cities = [ 
  new City(name: "Mui Ne"), 
  new City(name: "Hanoi")
]

cities.collect { it.name + ", VN" } == 
  [ "Mui Ne, VN", "Hanoi, VN" ]
cities.any { it.name.indexOf(" ") != -1 } == true
cities.every { it.name.indexOf(" ") == -1 } == false
cities.find { it.name.startsWith("H") }.name == "Hanoi"
​

Less code == Easier to read

The End

Groovify your Java code

By Hervé Roussel

Groovify your Java code

  • 1,814