SlideShare ist ein Scribd-Unternehmen logo
1 von 42
Downloaden Sie, um offline zu lesen
otlin 101
Christoph Pickl
Vienna, Austria – October 10th, 2016
A lighthouse on Kotlin Island, Russia
2
Introduction
Language fundamentals
Statically typed, hybrid programming language for the JVM
Fully interoperable with Java
Runs on Android (generates 1.6 bytecode)
Possibility to compile to JavaScript
Focuses on industry, tooling and safety
Open source compiler and tools (Apache 2 license)
4
Historic abstract
Developed by JetBrains (IntelliJ, ReSharper, WebStorm, . . . )
Development already started 2010
Since then used in projects at JetBrains, Prezi and Expedia
2M+ LoC and 100+ contributers at GitHub
Version 1.0 released February, 2016
Andrey Breslav
5
Yet another JVM language?
Source: RebelLabs
6
Java is dead, long live Java
“Most people talk about Java the language, and this may
sound odd coming from me, but I could hardly care less.
At the core of the Java ecosystem is the JVM.”
James Gosling,
Creator of the Java Programming Language (2011, TheServerSide)
7
8
Yet another JVM language!
Well known company with good tooling support
It got nothing new, just the best of all of them
It’s about the ecosystem, not the language
Empirical Analysis of Programming Language Adoption (PDF)
It’s really, really easy to learn
“Scala for dummies the masses”
9
Language Features
We’ll have a quick look at . . .
1 Type inference
2 Declarations
3 Lambdas
4 Null handling
5 Smart casts
6 Properties
7 Extension methods
8 Named & default arguments
9 Data classes
10 Collection API
11
Type inference
1 // mutable variable of type Int
2 var a = 42
3
4 // immutable value of type String
5 val b = "foobar"
6
7 // explicit type declaration
8 val c: Double = 13.37
9
10 // whatever the return type is
11 val d = someFunction ()
12
Function declaration
1 // global function with explicit return type
2 fun add1(x: Int , y: Int): Int {
3 return x + y
4 }
5
6 // compact one -line syntax
7 fun add2(x: Int , y: Int) = x + y
8
9 // possiblity to mark it as infix function
10 infix fun Int.add3(y: Int) = this + y
11 // enables us to write:
12 20 add3 22
13
Class declaration
1 // single ctor initializing a property
2 class Greeter(private val prefix: String) {
3
4 // string interpolation , no concatenation
5 fun greet(name: String) =
6 "$prefix $name!"
7 }
14
Object declaration
1 // an object is a singleton done properly
2 object Highlander : SwordFighter {
3 // mandatory ’override ’ keyword
4 override fun slash () { ... }
5 }
6
7 // looks like a nasty static invocation
8 Highlander.slash ()
9
10 // we only want to kill real sword fighters
11 fun killHim(fighter: SwordFighter) { ... }
12
13 // pass an (the!) instance reference
14 killHim(Highlander)
15
A simple application
Kotlin is well known for being a concise language:
1 // no surrounding class necessary
2 fun main(args: Array <String >) {
3
4 // no ’new’ keyword necessary
5 val greeter = Greeter("Hello")
6
7 // no ’System.out’ reference necessary
8 println(greeter.greet("JSUG"))
9 }
16
Lambdas compared
1 // with java 8:
2 Stream.of(1, 2, 3). filter(i -> i % 2 == 0)
3 .collect(Collectors.toList ());
4
5 // with kotlin:
6 listOf (1, 2, 3). filter { i -> i % 2 == 0 }
7
8 // or even shorter (groovy style ):
9 listOf (1, 2, 3). filter { it % 2 == 0 }
PS: If the last argument of a function is a function, it can be outside the
paranthesis; if there are no other arguments, you can even omit them.
17
Map function
1 fun <T, R> map(list: List <T>,
2 transform: (T) -> R): List <R> {
3 val result = arrayListOf <R>()
4 for (item in list)
5 result.add(transform(item ))
6 return result
7 }
8
9 // invoke the function and pass a lambda
10 map(listOf (1, 2, 3), { it * 2 })
18
19
Null handling
1 // nullable types for better compile checks
2 val maybe: String? = ...
3
4 maybe.length // COMPILE ERROR !!!
5 maybe ?. length // type is Int?
6 maybe ?. length ?: -1 // type is Int
7 maybe !!. length // i dont f*cking care!
8
9 if (maybe != null) {
10 maybe.length // smart cast to String
11 }
20
Smart casts
Java
Object x = ...;
if (x instanceof A) {
a = (A) x;
a.foo ();
} else if (x instanceof B) {
B b = (B) x;
b.bar ();
} else {
throw new Exception("Sad panda!");
}
Kotlin
val x: Any = ...
when (x) {
is A -> x.foo ()
is B -> x.bar ()
else -> throw Exception("Sad panda!")
// there are no checked exceptions :)
}
21
Pojo without Properties
1 public class Account {
2
3 private String id;
4 private int amount;
5
6 public Account(String id , int amount) {
7 this.id = id;
8 this.amount = amount;
9 }
10
11 public String getId () {
12 return id;
13 }
14 public void setId(String id) {
15 this.id = id;
16 }
17 public int getAmount () {
18 return amount;
19 }
20 public void setAmount(int amount) {
21 this.amount = amount;
22 }
23 }
22
Pojo with Properties
1 class Account {
2 var id: String? = null
3 var amount: Int = 0
4 }
5
6 // usage sample:
7 val account = Account ()
8 account.id = "asdf"
9 account.amount = 100
10 // invoke getter/setter from java as usual
23
Explicit Properties
1 class Account {
2 var amount = 0
3 get() = field // backing field
4 set(value) {
5 if (value < 0) throw SomeException ()
6 field = value
7 }
8 // ...
9 }
24
Compact Properties
1 // primary ctor initializing properties
2 class Account(
3 var id: String ,
4 var amount: Int
5 )
6 // no need for curly braces
25
Extension methods – The Problem
1 String agent = "7";
2
3 // we are used to call methods like this:
4 agent.length ();
5
6 // but as String is final we do this:
7 StringUtil.pad(agent , 3, "0");
How many Util classes are out there? A gazillion maybe?!
26
Extension methods – The Solution
1 fun String.pad(length: Int ,
2 symbol: String ): String {
3 // "this" refers to current string
4 ...
5 }
6
7 val agent = "7"
8 agent.pad(3, "0") // auto -completion FTW !!!
9
10 // nullable receiver transformation
11 fun Money ?. toDTO () =
12 if (this == null) null else MoneyDTO(this)
27
28
Extension Methods – Remarks
Known from C#, Smalltalk, Ruby and others
Add methods to yet compiled (final) classes
No syntactic difference between calling real vs extension
Actually simply creates static methods in background
Problem of delocalization solved, auto-completion works
Extension methods certainly are not object-oriented!
29
Default arguments
1 fun greet(
2 name: String ,
3 prefix: String = "Hello")
4 = "$prefix , ${name }!"
5
6 greet("JSUG", "Hi") => "Hi , JSUG!"
7
8 greet("JSUG") // => "Hello , JSUG !"
No more overloaded methods and delegation necessary!
30
Named arguments
1 fun greet(
2 name: String ,
3 prefix: String = "Hello",
4 suffix: String = "!")
5 { ... }
6
7 // specify the argument name and
8 // skip the "prefix" param (use default)
9 greet(name = "JSUG", suffix = ".")
31
Data classes
Java
public class Person {
private final String name;
private final int age;
public Person(String name , int age) {
this.name = name;
this.age = age;
}
public String getName () {
return name;
}
public int getAge () {
return age;
}
public boolean equals(Object other) {
...
}
public int hashCode () {
...
}
public String toString () {
...
}
}
Kotlin
data class Person(
val name: String ,
val age: Int
)
// same as with lombok but better ;)
// primary ctor deals with getters
// and field initialisation
// generates equals/hashCode/toString
// and a copy function (no builders !)
32
Collection API
Collections designed properly! (im/mutability as in Scala)
Create a map: mapOf("a" to 1, "b" to 2)
Access a map: map["a"]
size property for collections and arrays
Many, many useful (functional) extension methods . . .
Guava R.I.P., you served well
33
Collection API – Samples
1 // val list = listOf (1, 2, 3)
2 val list = mutableListOf (1, 2, 3)
3 list.add (42) // would not compile!
4 list. requireNoNulls ()
5 list.first () // => 1
6 list.firstOrNull { it > 10 } ?: -1
7 if (list.none { it > 10 })
8 println("no big ones")
9 list.toTypedArray ()
10
11 mapOf (1 to "a", 2 to "b", 3 to "a")
12 .values.distinct (). toHashSet ()
34
Further Features
Delegation
String handling
Sealed classes
Pattern matching
Reified generics
Operator overloading
if, try and when as expressions
== as equals
Range expressions
Rename imports
Backticks for escaping
. . . and much much more . . .
35
Epilog
Some Downsides
Still pretty young, but things are getting better
Some language features missing
Unsatisfying tool support (static code analysis)
Final by default is good, but bad for mocking
Mockito 2 FTW!
kotlinc is significantly slower than javac
37
Source: xkcd
38
Summary
Kotlin feels like Java 2.0 (Java on steroids)
It’s backed by a company and still open source
Pretty awesome community work
Version 1.0 just has been released (begin 2016)
Roadmap promises lots of improvements
39
Further reading
Try Kotlin online
http://try.kotlinlang.org
Learn by Koans
https://kotlinlang.org/docs/tutorials/koans.html
Ask, Discuss, Complain
https://kotlinlang.slack.com
Presentation Sources
https://github.com/christophpickl/kotlin101slides
Kotlin Vienna Usergroup
http://www.meetup.com/Kotlin-Vienna
40
41
Must have seen presentation by Dr. Russel Winder
42

Weitere ähnliche Inhalte

Was ist angesagt?

Introduction to basics of java
Introduction to basics of javaIntroduction to basics of java
Introduction to basics of javavinay arora
 
Kotlin presentation
Kotlin presentation Kotlin presentation
Kotlin presentation MobileAcademy
 
[COSCUP 2022] Kotlin Collection 遊樂園
[COSCUP 2022] Kotlin Collection 遊樂園[COSCUP 2022] Kotlin Collection 遊樂園
[COSCUP 2022] Kotlin Collection 遊樂園Shengyou Fan
 
Threading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines
Threading Made Easy! A Busy Developer’s Guide to Kotlin CoroutinesThreading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines
Threading Made Easy! A Busy Developer’s Guide to Kotlin CoroutinesLauren Yew
 
Kotlin vs Java | Edureka
Kotlin vs Java | EdurekaKotlin vs Java | Edureka
Kotlin vs Java | EdurekaEdureka!
 
Rust Programming Language
Rust Programming LanguageRust Programming Language
Rust Programming LanguageJaeju Kim
 
JavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for DummiesJavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for DummiesCharles Nutter
 
Learn Java with Dr. Rifat Shahriyar
Learn Java with Dr. Rifat ShahriyarLearn Java with Dr. Rifat Shahriyar
Learn Java with Dr. Rifat ShahriyarAbir Mohammad
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS ConceptRicha Gupta
 
Introduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platformIntroduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platformEastBanc Tachnologies
 
java tutorial for beginner - Free Download
java tutorial for beginner - Free Downloadjava tutorial for beginner - Free Download
java tutorial for beginner - Free DownloadTIB Academy
 
Kotlin as a Better Java
Kotlin as a Better JavaKotlin as a Better Java
Kotlin as a Better JavaGarth Gilmour
 
GraalVM Overview Compact version
GraalVM Overview Compact versionGraalVM Overview Compact version
GraalVM Overview Compact versionscalaconfjp
 
Kotlin for Android Development
Kotlin for Android DevelopmentKotlin for Android Development
Kotlin for Android DevelopmentSpeck&Tech
 
Introduction to java (revised)
Introduction to java (revised)Introduction to java (revised)
Introduction to java (revised)Sujit Majety
 

Was ist angesagt? (20)

Introduction to basics of java
Introduction to basics of javaIntroduction to basics of java
Introduction to basics of java
 
Kotlin presentation
Kotlin presentation Kotlin presentation
Kotlin presentation
 
[COSCUP 2022] Kotlin Collection 遊樂園
[COSCUP 2022] Kotlin Collection 遊樂園[COSCUP 2022] Kotlin Collection 遊樂園
[COSCUP 2022] Kotlin Collection 遊樂園
 
Kotlin - Better Java
Kotlin - Better JavaKotlin - Better Java
Kotlin - Better Java
 
Threading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines
Threading Made Easy! A Busy Developer’s Guide to Kotlin CoroutinesThreading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines
Threading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines
 
Kotlin vs Java | Edureka
Kotlin vs Java | EdurekaKotlin vs Java | Edureka
Kotlin vs Java | Edureka
 
Rust Programming Language
Rust Programming LanguageRust Programming Language
Rust Programming Language
 
LLVM introduction
LLVM introductionLLVM introduction
LLVM introduction
 
JavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for DummiesJavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for Dummies
 
Learn Java with Dr. Rifat Shahriyar
Learn Java with Dr. Rifat ShahriyarLearn Java with Dr. Rifat Shahriyar
Learn Java with Dr. Rifat Shahriyar
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS Concept
 
Introduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platformIntroduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platform
 
Android Virtualization: Opportunity and Organization
Android Virtualization: Opportunity and OrganizationAndroid Virtualization: Opportunity and Organization
Android Virtualization: Opportunity and Organization
 
java tutorial for beginner - Free Download
java tutorial for beginner - Free Downloadjava tutorial for beginner - Free Download
java tutorial for beginner - Free Download
 
Kotlin as a Better Java
Kotlin as a Better JavaKotlin as a Better Java
Kotlin as a Better Java
 
JDK,JRE,JVM
JDK,JRE,JVMJDK,JRE,JVM
JDK,JRE,JVM
 
GraalVM Overview Compact version
GraalVM Overview Compact versionGraalVM Overview Compact version
GraalVM Overview Compact version
 
Kotlin for Android Development
Kotlin for Android DevelopmentKotlin for Android Development
Kotlin for Android Development
 
Introduction to java (revised)
Introduction to java (revised)Introduction to java (revised)
Introduction to java (revised)
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 

Ähnlich wie Kotlin 101 for Java Developers

Game Design and Development Workshop Day 1
Game Design and Development Workshop Day 1Game Design and Development Workshop Day 1
Game Design and Development Workshop Day 1Troy Miles
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)Pavlo Baron
 
Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Cody Engel
 
Kotlin Language Features - A Java comparison
Kotlin Language Features - A Java comparisonKotlin Language Features - A Java comparison
Kotlin Language Features - A Java comparisonEd Austin
 
Kotlin, smarter development for the jvm
Kotlin, smarter development for the jvmKotlin, smarter development for the jvm
Kotlin, smarter development for the jvmArnaud Giuliani
 
Intro to Kotlin
Intro to KotlinIntro to Kotlin
Intro to KotlinMagda Miu
 
Kotlin: A pragmatic language by JetBrains
Kotlin: A pragmatic language by JetBrainsKotlin: A pragmatic language by JetBrains
Kotlin: A pragmatic language by JetBrainsJigar Gosar
 
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsBartosz Kosarzycki
 
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016STX Next
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecLoïc Descotte
 
What’s new in Kotlin?
What’s new in Kotlin?What’s new in Kotlin?
What’s new in Kotlin?Squareboat
 
Kotlin for Android Developers - 3
Kotlin for Android Developers - 3Kotlin for Android Developers - 3
Kotlin for Android Developers - 3Mohamed Nabil, MSc.
 
Building android apps with kotlin
Building android apps with kotlinBuilding android apps with kotlin
Building android apps with kotlinShem Magnezi
 
Kotlin: a better Java
Kotlin: a better JavaKotlin: a better Java
Kotlin: a better JavaNils Breunese
 
2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf
2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf
2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdfAndrey Breslav
 
Kotlin / Android Update
Kotlin / Android UpdateKotlin / Android Update
Kotlin / Android UpdateGarth Gilmour
 

Ähnlich wie Kotlin 101 for Java Developers (20)

Game Design and Development Workshop Day 1
Game Design and Development Workshop Day 1Game Design and Development Workshop Day 1
Game Design and Development Workshop Day 1
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)
 
Introduction to kotlin
Introduction to kotlinIntroduction to kotlin
Introduction to kotlin
 
Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)
 
Kotlin Language Features - A Java comparison
Kotlin Language Features - A Java comparisonKotlin Language Features - A Java comparison
Kotlin Language Features - A Java comparison
 
Kotlin, smarter development for the jvm
Kotlin, smarter development for the jvmKotlin, smarter development for the jvm
Kotlin, smarter development for the jvm
 
Intro to Kotlin
Intro to KotlinIntro to Kotlin
Intro to Kotlin
 
Just Kotlin
Just KotlinJust Kotlin
Just Kotlin
 
Kotlin: A pragmatic language by JetBrains
Kotlin: A pragmatic language by JetBrainsKotlin: A pragmatic language by JetBrains
Kotlin: A pragmatic language by JetBrains
 
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projects
 
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
 
What’s new in Kotlin?
What’s new in Kotlin?What’s new in Kotlin?
What’s new in Kotlin?
 
Kotlin for Android Developers - 3
Kotlin for Android Developers - 3Kotlin for Android Developers - 3
Kotlin for Android Developers - 3
 
Building android apps with kotlin
Building android apps with kotlinBuilding android apps with kotlin
Building android apps with kotlin
 
Kotlin: a better Java
Kotlin: a better JavaKotlin: a better Java
Kotlin: a better Java
 
Kotlin
KotlinKotlin
Kotlin
 
Why Kotlin is your next language?
Why Kotlin is your next language? Why Kotlin is your next language?
Why Kotlin is your next language?
 
2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf
2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf
2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf
 
Kotlin / Android Update
Kotlin / Android UpdateKotlin / Android Update
Kotlin / Android Update
 

Kürzlich hochgeladen

VictoriaMetrics Anomaly Detection Updates: Q1 2024
VictoriaMetrics Anomaly Detection Updates: Q1 2024VictoriaMetrics Anomaly Detection Updates: Q1 2024
VictoriaMetrics Anomaly Detection Updates: Q1 2024VictoriaMetrics
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...OnePlan Solutions
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slidesvaideheekore1
 
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxRTS corp
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesKrzysztofKkol1
 
SoftTeco - Software Development Company Profile
SoftTeco - Software Development Company ProfileSoftTeco - Software Development Company Profile
SoftTeco - Software Development Company Profileakrivarotava
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfRTS corp
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Anthony Dahanne
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecturerahul_net
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...Bert Jan Schrijver
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...OnePlan Solutions
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLionel Briand
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingShane Coughlan
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogueitservices996
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldRoberto Pérez Alcolea
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 

Kürzlich hochgeladen (20)

VictoriaMetrics Anomaly Detection Updates: Q1 2024
VictoriaMetrics Anomaly Detection Updates: Q1 2024VictoriaMetrics Anomaly Detection Updates: Q1 2024
VictoriaMetrics Anomaly Detection Updates: Q1 2024
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slides
 
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
 
SoftTeco - Software Development Company Profile
SoftTeco - Software Development Company ProfileSoftTeco - Software Development Company Profile
SoftTeco - Software Development Company Profile
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecture
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and Repair
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogue
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository world
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 

Kotlin 101 for Java Developers

  • 1. otlin 101 Christoph Pickl Vienna, Austria – October 10th, 2016
  • 2. A lighthouse on Kotlin Island, Russia 2
  • 4. Language fundamentals Statically typed, hybrid programming language for the JVM Fully interoperable with Java Runs on Android (generates 1.6 bytecode) Possibility to compile to JavaScript Focuses on industry, tooling and safety Open source compiler and tools (Apache 2 license) 4
  • 5. Historic abstract Developed by JetBrains (IntelliJ, ReSharper, WebStorm, . . . ) Development already started 2010 Since then used in projects at JetBrains, Prezi and Expedia 2M+ LoC and 100+ contributers at GitHub Version 1.0 released February, 2016 Andrey Breslav 5
  • 6. Yet another JVM language? Source: RebelLabs 6
  • 7. Java is dead, long live Java “Most people talk about Java the language, and this may sound odd coming from me, but I could hardly care less. At the core of the Java ecosystem is the JVM.” James Gosling, Creator of the Java Programming Language (2011, TheServerSide) 7
  • 8. 8
  • 9. Yet another JVM language! Well known company with good tooling support It got nothing new, just the best of all of them It’s about the ecosystem, not the language Empirical Analysis of Programming Language Adoption (PDF) It’s really, really easy to learn “Scala for dummies the masses” 9
  • 11. We’ll have a quick look at . . . 1 Type inference 2 Declarations 3 Lambdas 4 Null handling 5 Smart casts 6 Properties 7 Extension methods 8 Named & default arguments 9 Data classes 10 Collection API 11
  • 12. Type inference 1 // mutable variable of type Int 2 var a = 42 3 4 // immutable value of type String 5 val b = "foobar" 6 7 // explicit type declaration 8 val c: Double = 13.37 9 10 // whatever the return type is 11 val d = someFunction () 12
  • 13. Function declaration 1 // global function with explicit return type 2 fun add1(x: Int , y: Int): Int { 3 return x + y 4 } 5 6 // compact one -line syntax 7 fun add2(x: Int , y: Int) = x + y 8 9 // possiblity to mark it as infix function 10 infix fun Int.add3(y: Int) = this + y 11 // enables us to write: 12 20 add3 22 13
  • 14. Class declaration 1 // single ctor initializing a property 2 class Greeter(private val prefix: String) { 3 4 // string interpolation , no concatenation 5 fun greet(name: String) = 6 "$prefix $name!" 7 } 14
  • 15. Object declaration 1 // an object is a singleton done properly 2 object Highlander : SwordFighter { 3 // mandatory ’override ’ keyword 4 override fun slash () { ... } 5 } 6 7 // looks like a nasty static invocation 8 Highlander.slash () 9 10 // we only want to kill real sword fighters 11 fun killHim(fighter: SwordFighter) { ... } 12 13 // pass an (the!) instance reference 14 killHim(Highlander) 15
  • 16. A simple application Kotlin is well known for being a concise language: 1 // no surrounding class necessary 2 fun main(args: Array <String >) { 3 4 // no ’new’ keyword necessary 5 val greeter = Greeter("Hello") 6 7 // no ’System.out’ reference necessary 8 println(greeter.greet("JSUG")) 9 } 16
  • 17. Lambdas compared 1 // with java 8: 2 Stream.of(1, 2, 3). filter(i -> i % 2 == 0) 3 .collect(Collectors.toList ()); 4 5 // with kotlin: 6 listOf (1, 2, 3). filter { i -> i % 2 == 0 } 7 8 // or even shorter (groovy style ): 9 listOf (1, 2, 3). filter { it % 2 == 0 } PS: If the last argument of a function is a function, it can be outside the paranthesis; if there are no other arguments, you can even omit them. 17
  • 18. Map function 1 fun <T, R> map(list: List <T>, 2 transform: (T) -> R): List <R> { 3 val result = arrayListOf <R>() 4 for (item in list) 5 result.add(transform(item )) 6 return result 7 } 8 9 // invoke the function and pass a lambda 10 map(listOf (1, 2, 3), { it * 2 }) 18
  • 19. 19
  • 20. Null handling 1 // nullable types for better compile checks 2 val maybe: String? = ... 3 4 maybe.length // COMPILE ERROR !!! 5 maybe ?. length // type is Int? 6 maybe ?. length ?: -1 // type is Int 7 maybe !!. length // i dont f*cking care! 8 9 if (maybe != null) { 10 maybe.length // smart cast to String 11 } 20
  • 21. Smart casts Java Object x = ...; if (x instanceof A) { a = (A) x; a.foo (); } else if (x instanceof B) { B b = (B) x; b.bar (); } else { throw new Exception("Sad panda!"); } Kotlin val x: Any = ... when (x) { is A -> x.foo () is B -> x.bar () else -> throw Exception("Sad panda!") // there are no checked exceptions :) } 21
  • 22. Pojo without Properties 1 public class Account { 2 3 private String id; 4 private int amount; 5 6 public Account(String id , int amount) { 7 this.id = id; 8 this.amount = amount; 9 } 10 11 public String getId () { 12 return id; 13 } 14 public void setId(String id) { 15 this.id = id; 16 } 17 public int getAmount () { 18 return amount; 19 } 20 public void setAmount(int amount) { 21 this.amount = amount; 22 } 23 } 22
  • 23. Pojo with Properties 1 class Account { 2 var id: String? = null 3 var amount: Int = 0 4 } 5 6 // usage sample: 7 val account = Account () 8 account.id = "asdf" 9 account.amount = 100 10 // invoke getter/setter from java as usual 23
  • 24. Explicit Properties 1 class Account { 2 var amount = 0 3 get() = field // backing field 4 set(value) { 5 if (value < 0) throw SomeException () 6 field = value 7 } 8 // ... 9 } 24
  • 25. Compact Properties 1 // primary ctor initializing properties 2 class Account( 3 var id: String , 4 var amount: Int 5 ) 6 // no need for curly braces 25
  • 26. Extension methods – The Problem 1 String agent = "7"; 2 3 // we are used to call methods like this: 4 agent.length (); 5 6 // but as String is final we do this: 7 StringUtil.pad(agent , 3, "0"); How many Util classes are out there? A gazillion maybe?! 26
  • 27. Extension methods – The Solution 1 fun String.pad(length: Int , 2 symbol: String ): String { 3 // "this" refers to current string 4 ... 5 } 6 7 val agent = "7" 8 agent.pad(3, "0") // auto -completion FTW !!! 9 10 // nullable receiver transformation 11 fun Money ?. toDTO () = 12 if (this == null) null else MoneyDTO(this) 27
  • 28. 28
  • 29. Extension Methods – Remarks Known from C#, Smalltalk, Ruby and others Add methods to yet compiled (final) classes No syntactic difference between calling real vs extension Actually simply creates static methods in background Problem of delocalization solved, auto-completion works Extension methods certainly are not object-oriented! 29
  • 30. Default arguments 1 fun greet( 2 name: String , 3 prefix: String = "Hello") 4 = "$prefix , ${name }!" 5 6 greet("JSUG", "Hi") => "Hi , JSUG!" 7 8 greet("JSUG") // => "Hello , JSUG !" No more overloaded methods and delegation necessary! 30
  • 31. Named arguments 1 fun greet( 2 name: String , 3 prefix: String = "Hello", 4 suffix: String = "!") 5 { ... } 6 7 // specify the argument name and 8 // skip the "prefix" param (use default) 9 greet(name = "JSUG", suffix = ".") 31
  • 32. Data classes Java public class Person { private final String name; private final int age; public Person(String name , int age) { this.name = name; this.age = age; } public String getName () { return name; } public int getAge () { return age; } public boolean equals(Object other) { ... } public int hashCode () { ... } public String toString () { ... } } Kotlin data class Person( val name: String , val age: Int ) // same as with lombok but better ;) // primary ctor deals with getters // and field initialisation // generates equals/hashCode/toString // and a copy function (no builders !) 32
  • 33. Collection API Collections designed properly! (im/mutability as in Scala) Create a map: mapOf("a" to 1, "b" to 2) Access a map: map["a"] size property for collections and arrays Many, many useful (functional) extension methods . . . Guava R.I.P., you served well 33
  • 34. Collection API – Samples 1 // val list = listOf (1, 2, 3) 2 val list = mutableListOf (1, 2, 3) 3 list.add (42) // would not compile! 4 list. requireNoNulls () 5 list.first () // => 1 6 list.firstOrNull { it > 10 } ?: -1 7 if (list.none { it > 10 }) 8 println("no big ones") 9 list.toTypedArray () 10 11 mapOf (1 to "a", 2 to "b", 3 to "a") 12 .values.distinct (). toHashSet () 34
  • 35. Further Features Delegation String handling Sealed classes Pattern matching Reified generics Operator overloading if, try and when as expressions == as equals Range expressions Rename imports Backticks for escaping . . . and much much more . . . 35
  • 37. Some Downsides Still pretty young, but things are getting better Some language features missing Unsatisfying tool support (static code analysis) Final by default is good, but bad for mocking Mockito 2 FTW! kotlinc is significantly slower than javac 37
  • 39. Summary Kotlin feels like Java 2.0 (Java on steroids) It’s backed by a company and still open source Pretty awesome community work Version 1.0 just has been released (begin 2016) Roadmap promises lots of improvements 39
  • 40. Further reading Try Kotlin online http://try.kotlinlang.org Learn by Koans https://kotlinlang.org/docs/tutorials/koans.html Ask, Discuss, Complain https://kotlinlang.slack.com Presentation Sources https://github.com/christophpickl/kotlin101slides Kotlin Vienna Usergroup http://www.meetup.com/Kotlin-Vienna 40
  • 41. 41
  • 42. Must have seen presentation by Dr. Russel Winder 42