Groovify your java code by hervé roussel
-
Upload
herve-roussel -
Category
Technology
-
view
111 -
download
1
description
Transcript of Groovify your java code by hervé roussel
![Page 1: Groovify your java code by hervé roussel](https://reader034.fdocuments.us/reader034/viewer/2022042613/54b6b1084a7959ad7b8b4641/html5/thumbnails/1.jpg)
Groovify your Java Code
A guide to Groovy syntax for Java coders
By for Coding Dojo 2014
Interactive slides at:
Hervé Vũ Roussel Agile Vietnam
https://slides.com/hroussel/groovify-your-java-code
![Page 2: Groovify your java code by hervé roussel](https://reader034.fdocuments.us/reader034/viewer/2022042613/54b6b1084a7959ad7b8b4641/html5/thumbnails/2.jpg)
Print / bracket, semicol optional
System.out.println("Hello World!");
println "Hello World!"
![Page 3: Groovify your java code by hervé roussel](https://reader034.fdocuments.us/reader034/viewer/2022042613/54b6b1084a7959ad7b8b4641/html5/thumbnails/3.jpg)
Return optional
String getCurrentCity() { return "Ho Chi Minh City";}
def getCurrentCity() { "Ho Chi Minh City"}
![Page 4: Groovify your java code by hervé roussel](https://reader034.fdocuments.us/reader034/viewer/2022042613/54b6b1084a7959ad7b8b4641/html5/thumbnails/4.jpg)
Try/catch optional
try{ Reader reader = new FileReader("/vietnam-cities.txt")}catch(FileNotFoundException e) { e.printStackTrace()}
def reader = new FileReader("/vietnam-cities.txt")
![Page 5: Groovify your java code by hervé roussel](https://reader034.fdocuments.us/reader034/viewer/2022042613/54b6b1084a7959ad7b8b4641/html5/thumbnails/5.jpg)
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 = 1def d = 1.2
![Page 6: Groovify your java code by hervé roussel](https://reader034.fdocuments.us/reader034/viewer/2022042613/54b6b1084a7959ad7b8b4641/html5/thumbnails/6.jpg)
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 = 1Float f = 1.2
![Page 7: Groovify your java code by hervé roussel](https://reader034.fdocuments.us/reader034/viewer/2022042613/54b6b1084a7959ad7b8b4641/html5/thumbnails/7.jpg)
Existential operator (Elvis)
if (city != null) { if (city.getAirport() != null) { city.getAirport().getCode(); }}
city?.getAirport()?.getCode()
![Page 8: Groovify your java code by hervé roussel](https://reader034.fdocuments.us/reader034/viewer/2022042613/54b6b1084a7959ad7b8b4641/html5/thumbnails/8.jpg)
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"])
![Page 9: Groovify your java code by hervé roussel](https://reader034.fdocuments.us/reader034/viewer/2022042613/54b6b1084a7959ad7b8b4641/html5/thumbnails/9.jpg)
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
![Page 10: Groovify your java code by hervé roussel](https://reader034.fdocuments.us/reader034/viewer/2022042613/54b6b1084a7959ad7b8b4641/html5/thumbnails/10.jpg)
Ranges
List<String> alphabet = new ArrayList<String>();for(int i=0;i<26;i++) { alphabet.add(Character.toChars(i+97));}
def alphabet = "a".."z"
![Page 11: Groovify your java code by hervé roussel](https://reader034.fdocuments.us/reader034/viewer/2022042613/54b6b1084a7959ad7b8b4641/html5/thumbnails/11.jpg)
GString
System.out.println("Hello " + user.name);
println "Hello ${user.name}"
![Page 12: Groovify your java code by hervé roussel](https://reader034.fdocuments.us/reader034/viewer/2022042613/54b6b1084a7959ad7b8b4641/html5/thumbnails/12.jpg)
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
![Page 13: Groovify your java code by hervé roussel](https://reader034.fdocuments.us/reader034/viewer/2022042613/54b6b1084a7959ad7b8b4641/html5/thumbnails/13.jpg)
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
![Page 14: Groovify your java code by hervé roussel](https://reader034.fdocuments.us/reader034/viewer/2022042613/54b6b1084a7959ad7b8b4641/html5/thumbnails/14.jpg)
Compare objects
a.equals(b);a.compareTo(b);
a == ba < b
![Page 15: Groovify your java code by hervé roussel](https://reader034.fdocuments.us/reader034/viewer/2022042613/54b6b1084a7959ad7b8b4641/html5/thumbnails/15.jpg)
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")
![Page 16: Groovify your java code by hervé roussel](https://reader034.fdocuments.us/reader034/viewer/2022042613/54b6b1084a7959ad7b8b4641/html5/thumbnails/16.jpg)
Closures
List result = new ArrayList();for(city in city) { if (city.startsWith("H") result.add(city)}
cities.findAll { it.startsWith("H") }
![Page 17: Groovify your java code by hervé roussel](https://reader034.fdocuments.us/reader034/viewer/2022042613/54b6b1084a7959ad7b8b4641/html5/thumbnails/17.jpg)
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 } == truecities.every { it.name.indexOf(" ") == -1 } == falsecities.find { it.name.startsWith("H") }.name == "Hanoi"
![Page 18: Groovify your java code by hervé roussel](https://reader034.fdocuments.us/reader034/viewer/2022042613/54b6b1084a7959ad7b8b4641/html5/thumbnails/18.jpg)
![Page 19: Groovify your java code by hervé roussel](https://reader034.fdocuments.us/reader034/viewer/2022042613/54b6b1084a7959ad7b8b4641/html5/thumbnails/19.jpg)
The End
More on Groovy:
Book: (I didn't write it!)
Slides:Interactive:
PDF:
Thank you!
Free: Groovy User Guide
Groovy Recipes: Greasing the Wheels of Java
Online compiler/runner
http://slides.com/hroussel
https://www.slideshare.net/hroussel
Hervé Vũ Roussel
http://herveroussel.com
![Page 20: Groovify your java code by hervé roussel](https://reader034.fdocuments.us/reader034/viewer/2022042613/54b6b1084a7959ad7b8b4641/html5/thumbnails/20.jpg)
Groovify your Java CodeA guide to Groovy syntax for Java coders By Hervé Vũ Roussel for AgileVietnam Coding Dojo 2014 Interactive slides at: https://slides.com/hroussel/groovify-your-java-code
Groovify your Java codeEdit deck title & description
a day ago 0 68Hervé Roussel
http://www.herveroussel.com hvroussel
0Tweet
0 0
Like
We were unable to load Disqus. If you are a moderator please see our troubleshooting guide
0 Comments Slides
Disqus
Login
Newest
Oldest
Sort by Best
Best
Share
Share this discussion on
Start the discussion…