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
67
Android Testing Bootstrap, with Genymotion
eyallezmy
0
130
PlayStore Bashing: Learn from the big fails
eyallezmy
0
81
Android, the life of your app
eyallezmy
2
61
Build a User Experience on Android
eyallezmy
1
71
Secure your app, fight the leaks!
eyallezmy
0
37
Other Decks in Programming
See All in Programming
Figma Dev Modeで変わる!Flutterの開発体験
watanave
0
150
macOS でできる リアルタイム動画像処理
biacco42
9
2.4k
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
5
960
距離関数を極める! / SESSIONS 2024
gam0022
0
300
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
110
CSC509 Lecture 11
javiergs
PRO
0
180
OSSで起業してもうすぐ10年 / Open Source Conference 2024 Shimane
furukawayasuto
0
110
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
200
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
260
watsonx.ai Dojo #4 生成AIを使ったアプリ開発、応用編
oniak3ibm
PRO
1
190
ローコードSaaSのUXを向上させるためのTypeScript
taro28
1
650
React CompilerとFine Grained Reactivityと宣言的UIのこれから / The next chapter of declarative UI
ssssota
2
120
Featured
See All Featured
Measuring & Analyzing Core Web Vitals
bluesmoon
4
130
Thoughts on Productivity
jonyablonski
67
4.3k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
What's new in Ruby 2.0
geeforr
343
31k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
Music & Morning Musume
bryan
46
6.2k
Fireside Chat
paigeccino
34
3k
Speed Design
sergeychernyshev
25
620
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
GitHub's CSS Performance
jonrohan
1030
460k
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