Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
From Java to Groovy, the easy way
Search
Eyal LEZMY
December 22, 2014
Programming
1
110
From Java to Groovy, the easy way
This short presentation introduces to the Groovy language, coming from Java.
Eyal LEZMY
December 22, 2014
Tweet
Share
More Decks by Eyal LEZMY
See All by Eyal LEZMY
Gradle plugins, take it to the next level
eyallezmy
1
97
Gradle Plugin, take the control of the build!
eyallezmy
1
220
(Bitchy) App Clinic - Microsoft XIM
eyallezmy
0
68
Android Testing Bootstrap, with Genymotion
eyallezmy
0
130
PlayStore Bashing: Learn from the big fails
eyallezmy
1
81
Android, the life of your app
eyallezmy
2
62
Build a User Experience on Android
eyallezmy
1
72
Secure your app, fight the leaks!
eyallezmy
0
37
Other Decks in Programming
See All in Programming
선언형 UI에서의 상태관리
l2hyunwoo
0
270
functionalなアプローチで動的要素を排除する
ryopeko
1
340
Scaling your build logic
antalmonori
1
110
ATDDで素早く安定した デリバリを実現しよう!
tonnsama
1
2k
Lookerは可視化だけじゃない。UIコンポーネントもあるんだ!
ymd65536
1
130
asdf-ecspresso作って 友達が増えた話 / Fujiwara Tech Conference 2025
koluku
0
1.4k
20年もののレガシープロダクトに 0からPHPStanを入れるまで / phpcon2024
hirobe1999
0
1k
BEエンジニアがFEの業務をできるようになるまでにやったこと
yoshida_ryushin
0
210
「とりあえず動く」コードはよい、「読みやすい」コードはもっとよい / Code that 'just works' is good, but code that is 'readable' is even better.
mkmk884
6
1.4k
最近のVS Codeで気になるニュース 2025/01
74th
1
180
Fibonacci Function Gallery - Part 2
philipschwarz
PRO
0
220
Package Traits
ikesyo
1
210
Featured
See All Featured
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.5k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
GraphQLの誤解/rethinking-graphql
sonatard
68
10k
Bash Introduction
62gerente
610
210k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3.1k
The Cost Of JavaScript in 2023
addyosmani
46
7.2k
Testing 201, or: Great Expectations
jmmastey
41
7.2k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Agile that works and the tools we love
rasmusluckow
328
21k
Done Done
chrislema
182
16k
The Pragmatic Product Professional
lauravandoore
32
6.4k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
27
1.5k
Transcript
From Java to Groovy The easy way
Eyal LEZMY http://eyal.fr SLIDES http://bit.ly/JA2GR
What’s Groovy? Alternative language for the JVM Compiles bytecode Statically
or at Runtime Can use Java libraries
From Java to Groovy 01 by Guillaume Laforge
public class HelloWorld { private String name; public void setName(String
name) { this.name = name; } public String getName() { return name; } public String greet() { return "Hello " + name; } public static void main(String[] args) { HelloWorld helloWorld = new HelloWorld(); helloWorld.setName("Groovy"); System.out.println( helloWorld.greet() ); } } From JAVA
public class HelloWorld { private String name; public void setName(String
name) { this.name = name; } public String getName() { return name; } public String greet() { return "Hello " + name; } public static void main(String[] args) { HelloWorld helloWorld = new HelloWorld(); helloWorld.setName("Groovy"); System.out.println( helloWorld.greet() ); } } To GROOVY
public class HelloWorld { private String name; public void setName(String
name) { this.name = name; } public String getName() { return name; } public String greet() { return "Hello " + name; } public static void main(String[] args) { HelloWorld helloWorld = new HelloWorld(); helloWorld.setName("Groovy"); System.out.println( helloWorld.greet() ); } } To GROOVY
class HelloWorld { private String name void setName(String name) {
this.name = name } String getName() { return name } String greet() { return "Hello " + name } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.setName("Groovy") System.out.println( helloWorld.greet() ) } } To GROOVY
class HelloWorld { private String name void setName(String name) {
this.name = name } String getName() { return name } String greet() { return "Hello " + name } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.setName("Groovy") System.out.println( helloWorld.greet() ) } } To GROOVY
class HelloWorld { private String name void setName(String name) {
this.name = name } String getName() { return name } String greet() { return "Hello ${name}" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.setName("Groovy") System.out.println( helloWorld.greet() ) } } To GROOVY
class HelloWorld { private String name void setName(String name) {
this.name = name } String getName() { return name } String greet() { return "Hello ${name}" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.setName("Groovy") System.out.println( helloWorld.greet() ) } } To GROOVY GString => double quotes (“ ”)
class HelloWorld { private String name void setName(String name) {
this.name = name } String getName() { return name } String greet() { return "Hello $name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.setName("Groovy") System.out.println( helloWorld.greet() ) } } To GROOVY
class HelloWorld { private String name void setName(String name) {
this.name = name } String getName() { return name } String greet() { return "Hello $name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.setName("Groovy") System.out.println( helloWorld.greet() ) } } To GROOVY
class HelloWorld { private String name void setName(String name) {
this.name = name } String getName() { return name } String greet() { return "Hello $name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.setName("Groovy") System.out.println( helloWorld.greet() ) } } To GROOVY Private field
class HelloWorld { private String name void setName(String name) {
this.name = name } String getName() { return name } String greet() { return "Hello $name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.setName("Groovy") System.out.println( helloWorld.greet() ) } } To GROOVY Getter & Setter Private field
class HelloWorld { private String name void setName(String name) {
this.name = name } String getName() { return name } String greet() { return "Hello $name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.setName("Groovy") System.out.println( helloWorld.greet() ) } } To GROOVY
class HelloWorld { String name String greet() { return "Hello
$name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.setName("Groovy") System.out.println( helloWorld.greet() ) } } To GROOVY
class HelloWorld { String name String greet() { return "Hello
$name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.setName("Groovy") System.out.println( helloWorld.greet() ) } } To GROOVY
class HelloWorld { String name String greet() { return "Hello
$name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.setName("Groovy") System.out.println( helloWorld.greet() ) } } To GROOVY
class HelloWorld { String name String greet() { return "Hello
$name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.name = "Groovy" System.out.println( helloWorld.greet() ) } } To GROOVY
class HelloWorld { String name String greet() { return "Hello
$name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.name = "Groovy" System.out.println( helloWorld.greet() ) } } To GROOVY
class HelloWorld { String name String greet() { return "Hello
$name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.name = "Groovy" System.out.println( helloWorld.greet() ) } } To GROOVY
class HelloWorld { String name String greet() { return "Hello
$name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.name = "Groovy" println( helloWorld.greet() ) } } To GROOVY
class HelloWorld { String name String greet() { return "Hello
$name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.name = "Groovy" println( helloWorld.greet() ) } } To GROOVY
class HelloWorld { String name String greet() { return "Hello
$name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.name = "Groovy" println helloWorld.greet() } } To GROOVY
class HelloWorld { String name String greet() { return "Hello
$name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.name = "Groovy" println helloWorld.greet() } } To GROOVY
class HelloWorld { def name def greet() { return "Hello
$name" } static main(args) { def helloWorld = new HelloWorld() helloWorld.name = "Groovy" println helloWorld.greet() } } To GROOVY
class HelloWorld { def name def greet() { return "Hello
$name" } static main(args) { def helloWorld = new HelloWorld() helloWorld.name = "Groovy" println helloWorld.greet() } } To GROOVY
class HelloWorld { def name def greet() { return "Hello
$name" } static main(args) { def helloWorld = new HelloWorld() helloWorld.name = "Groovy" println helloWorld.greet() } } To GROOVY
class HelloWorld { def name def greet() { return "Hello
$name" } } def helloWorld = new HelloWorld() helloWorld.name = "Groovy" println helloWorld.greet() To GROOVY
public class HelloWorld { private String name; public void setName(String
name) { this.name = name; } public String getName() { return name; } public String greet() { return "Hello " + name; } public static void main(String[] args) { HelloWorld helloWorld = new HelloWorld(); helloWorld.setName("Groovy"); System.out.println( helloWorld.greet() ); } } From JAVA
Where the Magic happens... 02
Methods Default value parameters Named parameters No return statement Return
multiple variables Groovy Methods
def createGenyboy(gender = “male”) { … if(gender == “male”) name
= “Genyboy” … [name, color, size] } def(String name, Color color, int size) = createGenyboy(gender:“female”) Groovy Methods
static def cmd(def command, Closure c){ Process p = command.execute()
p.text.eachLine {line, count -> c(line, count) } p.exitValue() } a = “hello” cmd(“gmtool admin list”) {line, count -> print a + line } Groovy Closures
def array = [] array = [“one”, “two”, “three”] tab.findAll(){item
-> item.size == 3 print it } [1, 2, 3].each(){ print it } def map = [:] map = [one:1, two:2, three:3] assert map.one == map[“one”] def range = 2..5 assert range == [2, 3, 4, 5] Groovy Collections
static noNull(Closure c){ String nullLabel = null.toString() NullObject.metaClass.toString = {return
''} c() //set as defaut NullObject.metaClass.toString = {return nullLabel} } cmd([“gmtool”, “admin”, “create” “template”, “device”, “--density=”+density]) >> gmtool admin create template device --density=null noNull{ cmd([“gmtool”, “admin”, “create” “template”, “device”, “--density=”+density]) } >> gmtool admin create template device --density= Groovy Metaclass
boolean checkTable(tab) { if(tab?.size() >= 4) return true return false
} Groovy Safe Navigation
And others Native support for Regex ~/myRegex/ Powerful switch statement
switch awesomness { case 1..10: “yeah!” } Operator Overloading 1 + 1 = 1
Conclusion You keep focus on business logic Like magic So
dynamic, you have to test ! JUnit & Co. So dynamic, so easy to mock JSON-likery
References From Java to Groovy http://groovy.dzone.com/news/java-groovy-few-easy-steps By Guillaume Laforge Differences
from Java http://groovy.codehaus.org/Differences+from+Java Official documentation http://groovy.codehaus.org/
Thank you for your time ! http://eyal.fr SLIDES http://bit.ly/JA2GR