SlideShare uma empresa Scribd logo
1 de 29
Baixar para ler offline
Kotlin for Java Developers
what every java developer should know about kotlin
Today I would like to share with you
● Why I care about Kotlin?
● 20 features I love
● 10 features you need to be aware of
● Ecosystem
Next JVM language?
● Statically typed programming language for multi platform applications
○ concise
○ safe
○ interoperable with Java
○ built together with tooling support
○ open source under Apache 2.0 license
● Version 1.0 released in 2015, current version 1.1.50 (September 2017)
● Google announced first-class support for Kotlin on Android
● Pivotal introduced Kotlin support in Spring Framework 5.0
● Easy to learn if you know Java
Kotlin basics
Features I love
Extension functions and properties
Data classes
● equals() / hashCode() pair
● toString() “Account[email=test]”
● componentN() functions in their order
of declaration
● copy() function
String templates
Null safety
● Types defines nullability
○ Platform types
● Safe calls
● Elvis operator
● !!. operator
● Safe casts
when
Operator overloading
● +, -, *, /, %, ..
○ a + b -> a.plus(b)
○ a..b -> a.rangeTo(b)
● in, !in
○ a.contains(b)
● Indexed access []
○ a[i] -> a.get(i)
○ a[i] = b -> a.set(i, b)
● Invoke
○ a(i, j) -> a.invoke(i, j)
● a == b
○ a?.equals(b) ?: (b === null)
● a > b, a < b, a >= b, a <= b -> a.compareTo(b)
Operator overloading (1)
Default and named parameters
Smart casts
Destructuring objects
● underscore for unused
variables (1.1)
● destructuring in lambdas
(1.1)
Lambda and closures
Expressions and statements
● if and when are expressions, not statements
○ val length = if (a is String) a.length else -1
○ val action = when (test) {
in 0..5 -> OPEN
else -> CLOSE
}
● assignment is a statement, not an expression
○ if (a = b) does not compile
○ while ((line = bufferedReader.readLine()) != null) does not compile
Packages and source code structure
● packages
● import allows to import classes, functions, *
● type aliases
● multiple classes in one file
● arbitrary file names
● arbitrary directory structure
● visibility modifiers: private, protected, internal, public
Other languages have all these features
● Null safety
● No checked exceptions
● Extension functions
● Function types and lambdas
● Default and named parameters
● Properties
● Operator overloading
● Smart casts
● Data classes
● Immutable collections
● Enhanced switch-case
● String templates
● Ranges
● Infix notation
● Inline functions
● Coroutines (async/await)
● Great standard library
● Sealed classes
● Delegated and lazy properties
● Class delegation
● Singletons
● Nested functions
● Object decomposition
● Top-level functions
● Reified generics
● Raw strings
● 100% interoperable with Java 6
● And more...
Compile and run with Java code
● you can mix Java and Kotlin code in one project
● experiment with new language without breaking or rewriting the whole
application
● small memory footprint of the Kotlin standard library
Understand decisions
What you need to know
● final by default and bytecode generation
● platform types and nullability
● no primitives, no implicit widening conversions for numbers
● standard library
Final by default and bytecode generation
● all classes, methods are final by default
○ tedious opening via ‘open’ keyword
○ interference with AOP (CGLIB), workarounds as compiler plugins ‘kotlin-spring’, ‘all-open’
● ‘override’ is a required keyword, not an annotation
● designing for inheritance
● kotlinc generates Java 6 or Java 8 bytecode
● on JVM lambdas does not use ‘invokedynamic’
● Kotlin Bytecode Generation and Runtime Performance
Platform types and nullability (!)
● any reference in Java may be null
● types of Java declarations are called platform types
● can be assigned to nullable or non-null type
● compiler, tools refers to them using as T! which means T ot T?
● nullability annotations (JSR-305, Android, Lombok, JetBrains, Eclipse)
Standard library
● kotlin-runtime and kotlin-stdlib -> kotlin-stdlib
○ < 1MB jar (JVM)
● Kotlin classes and extension functions to Java classes
○ kotlin
○ kotlin.collections
○ kotlin.comparisons
○ kotlin.concurrent
○ kotlin.io
○ kotlin.streams
○ kotlin.text
● kotlin.js
Platforms and tooling
Platforms and tooling
● JVM
a. Java 6 and 8
● Android
● JavaScript (ES5.1)
a. compatible with module systems like AMD, CommonJS
● native (LLVM)
a. LLVM is used to compile Kotlin into native code
b. technology preview for iOS, linux, MAC, (windows in the work)
● IntelliJ IDEA (Java to Kotlin converter), Eclipse
● Gradle, Maven, Ant
Where to start?
● Try online https://try.kotlinlang.org/
● Kotlin is Awesome! https://kotlin.link/
What will be your next JVM
language?
https://en.wikipedia.org/wiki/List_of_JVM_languages

Mais conteúdo relacionado

Mais procurados

Object Oriented Programming : Part 2
Object Oriented Programming : Part 2Object Oriented Programming : Part 2
Object Oriented Programming : Part 2Madhavan Malolan
 
Clang Analyzer Tool Review
Clang Analyzer Tool ReviewClang Analyzer Tool Review
Clang Analyzer Tool ReviewDoug Schuster
 
Phpactor and VIM
Phpactor and VIMPhpactor and VIM
Phpactor and VIMdantleech
 
Introduction to Kotlin JVM language
Introduction to Kotlin JVM languageIntroduction to Kotlin JVM language
Introduction to Kotlin JVM languageAndrius Klimavicius
 
Getting Started with PHP Extensions
Getting Started with PHP ExtensionsGetting Started with PHP Extensions
Getting Started with PHP ExtensionsMichaelBrunoLochemem
 
Kotlin & Arrow the functional way
Kotlin & Arrow the functional wayKotlin & Arrow the functional way
Kotlin & Arrow the functional wayThoughtworks
 
A Shallow Survey of Alternative Languages on the JVM
A Shallow Survey of Alternative Languages on the JVMA Shallow Survey of Alternative Languages on the JVM
A Shallow Survey of Alternative Languages on the JVMCharles Anderson
 
Modern Programming Languages - An overview
Modern Programming Languages - An overviewModern Programming Languages - An overview
Modern Programming Languages - An overviewAyman Mahfouz
 
Kotlin & arrow: the functional way
Kotlin & arrow:  the functional wayKotlin & arrow:  the functional way
Kotlin & arrow: the functional waynluaces
 
Kotlin - A Programming Language
Kotlin - A Programming Language Kotlin - A Programming Language
Kotlin - A Programming Language Mobio Solutions
 
Test-driven language development
Test-driven language developmentTest-driven language development
Test-driven language developmentlennartkats
 
The Spoofax Language Workbench (SPLASH 2010)
The Spoofax Language Workbench (SPLASH 2010)The Spoofax Language Workbench (SPLASH 2010)
The Spoofax Language Workbench (SPLASH 2010)lennartkats
 
.NET compiler platform codename Roslyn
.NET compiler platform codename Roslyn.NET compiler platform codename Roslyn
.NET compiler platform codename RoslynPiotr Benetkiewicz
 
Lock-free algorithms for Kotlin Coroutines
Lock-free algorithms for Kotlin CoroutinesLock-free algorithms for Kotlin Coroutines
Lock-free algorithms for Kotlin CoroutinesRoman Elizarov
 

Mais procurados (20)

Object Oriented Programming : Part 2
Object Oriented Programming : Part 2Object Oriented Programming : Part 2
Object Oriented Programming : Part 2
 
Ruxmon.2013-08.-.CodeBro!
Ruxmon.2013-08.-.CodeBro!Ruxmon.2013-08.-.CodeBro!
Ruxmon.2013-08.-.CodeBro!
 
Clang Analyzer Tool Review
Clang Analyzer Tool ReviewClang Analyzer Tool Review
Clang Analyzer Tool Review
 
Phpactor and VIM
Phpactor and VIMPhpactor and VIM
Phpactor and VIM
 
Introduction to Kotlin JVM language
Introduction to Kotlin JVM languageIntroduction to Kotlin JVM language
Introduction to Kotlin JVM language
 
Getting Started with PHP Extensions
Getting Started with PHP ExtensionsGetting Started with PHP Extensions
Getting Started with PHP Extensions
 
Kotlin & Arrow the functional way
Kotlin & Arrow the functional wayKotlin & Arrow the functional way
Kotlin & Arrow the functional way
 
A Shallow Survey of Alternative Languages on the JVM
A Shallow Survey of Alternative Languages on the JVMA Shallow Survey of Alternative Languages on the JVM
A Shallow Survey of Alternative Languages on the JVM
 
Modern Programming Languages - An overview
Modern Programming Languages - An overviewModern Programming Languages - An overview
Modern Programming Languages - An overview
 
Android with kotlin course
Android with kotlin courseAndroid with kotlin course
Android with kotlin course
 
Kotlin & arrow: the functional way
Kotlin & arrow:  the functional wayKotlin & arrow:  the functional way
Kotlin & arrow: the functional way
 
Kotlin - A Programming Language
Kotlin - A Programming Language Kotlin - A Programming Language
Kotlin - A Programming Language
 
Coding in kotlin
Coding in kotlinCoding in kotlin
Coding in kotlin
 
Java 8 Lambda
Java 8 LambdaJava 8 Lambda
Java 8 Lambda
 
Clojure presentation
Clojure presentationClojure presentation
Clojure presentation
 
Test-driven language development
Test-driven language developmentTest-driven language development
Test-driven language development
 
TypeScript intro
TypeScript introTypeScript intro
TypeScript intro
 
The Spoofax Language Workbench (SPLASH 2010)
The Spoofax Language Workbench (SPLASH 2010)The Spoofax Language Workbench (SPLASH 2010)
The Spoofax Language Workbench (SPLASH 2010)
 
.NET compiler platform codename Roslyn
.NET compiler platform codename Roslyn.NET compiler platform codename Roslyn
.NET compiler platform codename Roslyn
 
Lock-free algorithms for Kotlin Coroutines
Lock-free algorithms for Kotlin CoroutinesLock-free algorithms for Kotlin Coroutines
Lock-free algorithms for Kotlin Coroutines
 

Semelhante a JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)

Understand What and Why Kotlin.pptx
Understand What and Why Kotlin.pptxUnderstand What and Why Kotlin.pptx
Understand What and Why Kotlin.pptxAnkitkumar822995
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scaladatamantra
 
Kotlin workshop 2018-06-11
Kotlin workshop 2018-06-11Kotlin workshop 2018-06-11
Kotlin workshop 2018-06-11Åsa Pehrsson
 
Java Presentation For Syntax
Java Presentation For SyntaxJava Presentation For Syntax
Java Presentation For SyntaxPravinYalameli
 
Exploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App developmentExploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App developmentJayaprakash R
 
Testing with Express, Mocha & Chai
Testing with Express, Mocha & ChaiTesting with Express, Mocha & Chai
Testing with Express, Mocha & ChaiJoerg Henning
 
Python assignment help from professional programmers
Python assignment help from professional programmersPython assignment help from professional programmers
Python assignment help from professional programmersAnderson Silva
 
Dart the Better JavaScript
Dart the Better JavaScriptDart the Better JavaScript
Dart the Better JavaScriptJorg Janke
 
A quick and fast intro to Kotlin
A quick and fast intro to Kotlin A quick and fast intro to Kotlin
A quick and fast intro to Kotlin XPeppers
 
Smalltalk, the dynamic language
Smalltalk, the dynamic languageSmalltalk, the dynamic language
Smalltalk, the dynamic languagemohamedsamyali
 
Future of Kotlin - How agile can language development be?
Future of Kotlin - How agile can language development be?Future of Kotlin - How agile can language development be?
Future of Kotlin - How agile can language development be?Andrey Breslav
 
Kotlin Language powerpoint show file
Kotlin Language powerpoint show fileKotlin Language powerpoint show file
Kotlin Language powerpoint show fileSaurabh Tripathi
 

Semelhante a JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński) (20)

Introduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTXIntroduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTX
 
Why Kotlin?
Why Kotlin?Why Kotlin?
Why Kotlin?
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
 
Kotlin tlv
Kotlin tlvKotlin tlv
Kotlin tlv
 
Understand What and Why Kotlin.pptx
Understand What and Why Kotlin.pptxUnderstand What and Why Kotlin.pptx
Understand What and Why Kotlin.pptx
 
Core Java Tutorial
Core Java TutorialCore Java Tutorial
Core Java Tutorial
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scala
 
Kotlin workshop 2018-06-11
Kotlin workshop 2018-06-11Kotlin workshop 2018-06-11
Kotlin workshop 2018-06-11
 
Java Presentation For Syntax
Java Presentation For SyntaxJava Presentation For Syntax
Java Presentation For Syntax
 
Exploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App developmentExploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App development
 
Testing with Express, Mocha & Chai
Testing with Express, Mocha & ChaiTesting with Express, Mocha & Chai
Testing with Express, Mocha & Chai
 
Coroutines in Kotlin
Coroutines in KotlinCoroutines in Kotlin
Coroutines in Kotlin
 
Python assignment help from professional programmers
Python assignment help from professional programmersPython assignment help from professional programmers
Python assignment help from professional programmers
 
Dart the Better JavaScript
Dart the Better JavaScriptDart the Better JavaScript
Dart the Better JavaScript
 
A quick and fast intro to Kotlin
A quick and fast intro to Kotlin A quick and fast intro to Kotlin
A quick and fast intro to Kotlin
 
Smalltalk, the dynamic language
Smalltalk, the dynamic languageSmalltalk, the dynamic language
Smalltalk, the dynamic language
 
Future of Kotlin - How agile can language development be?
Future of Kotlin - How agile can language development be?Future of Kotlin - How agile can language development be?
Future of Kotlin - How agile can language development be?
 
Unit 1
Unit 1Unit 1
Unit 1
 
Quality Assurance in PostgreSQL
Quality Assurance in PostgreSQLQuality Assurance in PostgreSQL
Quality Assurance in PostgreSQL
 
Kotlin Language powerpoint show file
Kotlin Language powerpoint show fileKotlin Language powerpoint show file
Kotlin Language powerpoint show file
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 

Último (20)

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)

  • 1. Kotlin for Java Developers what every java developer should know about kotlin
  • 2. Today I would like to share with you ● Why I care about Kotlin? ● 20 features I love ● 10 features you need to be aware of ● Ecosystem
  • 3. Next JVM language? ● Statically typed programming language for multi platform applications ○ concise ○ safe ○ interoperable with Java ○ built together with tooling support ○ open source under Apache 2.0 license ● Version 1.0 released in 2015, current version 1.1.50 (September 2017) ● Google announced first-class support for Kotlin on Android ● Pivotal introduced Kotlin support in Spring Framework 5.0 ● Easy to learn if you know Java
  • 7. Data classes ● equals() / hashCode() pair ● toString() “Account[email=test]” ● componentN() functions in their order of declaration ● copy() function
  • 9. Null safety ● Types defines nullability ○ Platform types ● Safe calls ● Elvis operator ● !!. operator ● Safe casts
  • 10. when
  • 11. Operator overloading ● +, -, *, /, %, .. ○ a + b -> a.plus(b) ○ a..b -> a.rangeTo(b) ● in, !in ○ a.contains(b) ● Indexed access [] ○ a[i] -> a.get(i) ○ a[i] = b -> a.set(i, b) ● Invoke ○ a(i, j) -> a.invoke(i, j) ● a == b ○ a?.equals(b) ?: (b === null) ● a > b, a < b, a >= b, a <= b -> a.compareTo(b)
  • 13. Default and named parameters
  • 15. Destructuring objects ● underscore for unused variables (1.1) ● destructuring in lambdas (1.1)
  • 17. Expressions and statements ● if and when are expressions, not statements ○ val length = if (a is String) a.length else -1 ○ val action = when (test) { in 0..5 -> OPEN else -> CLOSE } ● assignment is a statement, not an expression ○ if (a = b) does not compile ○ while ((line = bufferedReader.readLine()) != null) does not compile
  • 18. Packages and source code structure ● packages ● import allows to import classes, functions, * ● type aliases ● multiple classes in one file ● arbitrary file names ● arbitrary directory structure ● visibility modifiers: private, protected, internal, public
  • 19. Other languages have all these features ● Null safety ● No checked exceptions ● Extension functions ● Function types and lambdas ● Default and named parameters ● Properties ● Operator overloading ● Smart casts ● Data classes ● Immutable collections ● Enhanced switch-case ● String templates ● Ranges ● Infix notation ● Inline functions ● Coroutines (async/await) ● Great standard library ● Sealed classes ● Delegated and lazy properties ● Class delegation ● Singletons ● Nested functions ● Object decomposition ● Top-level functions ● Reified generics ● Raw strings ● 100% interoperable with Java 6 ● And more...
  • 20. Compile and run with Java code ● you can mix Java and Kotlin code in one project ● experiment with new language without breaking or rewriting the whole application ● small memory footprint of the Kotlin standard library
  • 22. What you need to know ● final by default and bytecode generation ● platform types and nullability ● no primitives, no implicit widening conversions for numbers ● standard library
  • 23. Final by default and bytecode generation ● all classes, methods are final by default ○ tedious opening via ‘open’ keyword ○ interference with AOP (CGLIB), workarounds as compiler plugins ‘kotlin-spring’, ‘all-open’ ● ‘override’ is a required keyword, not an annotation ● designing for inheritance ● kotlinc generates Java 6 or Java 8 bytecode ● on JVM lambdas does not use ‘invokedynamic’ ● Kotlin Bytecode Generation and Runtime Performance
  • 24. Platform types and nullability (!) ● any reference in Java may be null ● types of Java declarations are called platform types ● can be assigned to nullable or non-null type ● compiler, tools refers to them using as T! which means T ot T? ● nullability annotations (JSR-305, Android, Lombok, JetBrains, Eclipse)
  • 25. Standard library ● kotlin-runtime and kotlin-stdlib -> kotlin-stdlib ○ < 1MB jar (JVM) ● Kotlin classes and extension functions to Java classes ○ kotlin ○ kotlin.collections ○ kotlin.comparisons ○ kotlin.concurrent ○ kotlin.io ○ kotlin.streams ○ kotlin.text ● kotlin.js
  • 27. Platforms and tooling ● JVM a. Java 6 and 8 ● Android ● JavaScript (ES5.1) a. compatible with module systems like AMD, CommonJS ● native (LLVM) a. LLVM is used to compile Kotlin into native code b. technology preview for iOS, linux, MAC, (windows in the work) ● IntelliJ IDEA (Java to Kotlin converter), Eclipse ● Gradle, Maven, Ant
  • 28. Where to start? ● Try online https://try.kotlinlang.org/ ● Kotlin is Awesome! https://kotlin.link/
  • 29. What will be your next JVM language? https://en.wikipedia.org/wiki/List_of_JVM_languages