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
120
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
From Java to Groovy, the easy way
This short presentation introduces to the Groovy language, coming from Java.
Eyal LEZMY
December 22, 2014
More Decks by Eyal LEZMY
See All by Eyal LEZMY
Gradle plugins, take it to the next level
eyallezmy
1
110
Gradle Plugin, take the control of the build!
eyallezmy
1
230
(Bitchy) App Clinic - Microsoft XIM
eyallezmy
0
81
Android Testing Bootstrap, with Genymotion
eyallezmy
0
140
PlayStore Bashing: Learn from the big fails
eyallezmy
1
88
Android, the life of your app
eyallezmy
2
75
Build a User Experience on Android
eyallezmy
1
81
Secure your app, fight the leaks!
eyallezmy
0
48
Other Decks in Programming
See All in Programming
琵琶湖の水は止められてもNet--HTTPのリトライは止められない / You might be able to stop the water flow of Lake Biwa but you can't stop Net::HTTP retries
luccafort
PRO
0
640
プロポーザルを書いてもらう
pvcresin
0
300
torikago - Ruby::Boxで照らすモジュラモノリスの実行境界
se4weed
1
360
関東Kaggler会_NVIDIA_Nemotron_コンペ_振り返り
rick_ds
0
510
yield再入門 #phpcon
o0h
PRO
0
1k
the container ship “Apple Silicon”@WWDC26 Recap -Japan-\(region).swift
shingangan
0
120
言語を使う側から、作る側へ。 自作 Lisp で得た新たな気づき。
andpad
0
160
Built Our Own Background Agent at LayerX
layerx
PRO
9
5.2k
為什麼你並不需要ViewModel / No, you don't need a ViewModel
lovee
1
490
Detecting Compromised CI with eBPF and Cilium Tetragon
lizrice
0
170
React本体のコードリーディング
high_g_engineer
1
140
【やさしく解説 設計編・中級 #1】一つの車に、運転手は一人 ~ある倉庫システムの事例から~
panda728
PRO
0
210
Featured
See All Featured
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
45k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
290
Design in an AI World
tapps
1
280
Faster Mobile Websites
deanohume
310
32k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
How to train your dragon (web standard)
notwaldorf
97
6.7k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.5k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
520
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
56
3.4k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
The browser strikes back
jonoalderson
0
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