SlideShare une entreprise Scribd logo
1  sur  35
Télécharger pour lire hors ligne
for Java developers
A short introduction
to the Kotlin language
Antonis Lilis
Mobile Engineer @
JHUG Meetup
February 2018
Some History
● 2011: JetBrains unveiled Project Kotlin, a new language for the JVM
● 2012: JetBrains open sourced the project under the Apache 2 license
● 2016: Kotlin v1.0 is released
● 2017: Google announced first-class support for Kotlin on Android
● Kotlin is technically 7, but in reality 2 years old
Trivia: The name comes from a small island in the
Baltic Sea, near St.Petersburg. The language team
decided to name it after an island just like Java
(though Java was perhaps named after the coffee)
The Kotlin Language
● Statically Typed
○ Type validation at compile time
● Supports Type Inference
○ Type automatically determined from the context
● Both Object Oriented and Functional
● First-class functions
○ You can store them in variables, pass them as parameters, or return them
from other functions
● Was designed with Java Interoperability in mind
Constants and Variables
● val (from value)
○ Immutable reference
● var (from variable)
○ Mutable reference
● Nullable Types
○ Defined Explicitly
No
semicolon
here ;)
Control Flow
● Classic loops:
○ if
○ for
○ while / do-while
● when
○ Replaces the switch operator
○ No breaks, no errors
Functions
● Named arguments
● Can be declared at the top level of a file (without belonging to a class)
● Can be Nested
● Can have a block or expression body
Functions
● Default parameter values
○ Avoids method overloading and boilerplate code
Simple string
Interpolation
Classes
data classes:
autogenerated
implementations of
universal methods
(equals, hashCode
etc)
“Any” is the
analogue of java
Object: a superclass
of all classes
Properties
● first-class language feature
● combination of the field
and its accessors
Modifiers
● Access modifiers
○ final (default)
○ open
○ abstract
● Visibility modifiers
○ public (default)
○ internal
○ protected
○ private
"Design and document for inheritance
or else prohibit it"
Joshua J. Bloch, Effective Java
ps. Lukas Lechner has written a series of articles on
"How Effective Java influenced Kotlin" (http://lukle.at)
No static keyword
● Top-level functions and properties
(e.g. for utility classes)
● Companion objects
● The object keyword:
declaring a class and creating an instance
combined (Singleton)
Extensions
● Enable adding methods and properties to other people’s classes
○ Of Course without access to private or protected members of the class
Null Checks
● Safe-call operator ?.
● Elvis operator ?:
● The let function
"I call it my billion-dollar mistake.
It was the invention of the null reference in 1965"
Tony Hoare
Not-null assertion operator !!
Safe Casting
● Safe cast operator as?
● Smart cast
○ combining type checks
and casts
Collections
● Kotlin enhances the Java collection classes (List, Set, Map)
Chained
Calls
Delegation
● Composition over Inheritance
design pattern
● Native support for delegation
(implicit delegation)
● Zero Boilerplate code
● Supports both Class Delegation and
Delegated Properties
Class Car inherits from an interface Nameable
and delegates all of its public methods to a
delegate object defined with the by keyword
Lamdas and Higher Order Functions
Domain-specific language construction
● Kotlin provides mechanisms for creating internal DSLs that use exactly the
same syntax as all language features and are fully statically typed
Coroutines
● Introduced in Kotlin 1.1 (March 2017)
● A way to write asynchronous code sequentially
● Multithreading in a way that is easily debuggable and maintainable
● Based on the idea of suspending function execution
● More lightweight and efficient than threads
Source Code Layout
● Packages (similar to that in Java)
● Multiple classes can fit in the same file
● You can choose any name for files (not restricted to class name)
● The import keyword is not restricted to importing classes
○ Top-level functions and properties can be imported
● Kotlin does not impose any restrictions on the layout of source files on disk
● Good practice to follow Java’s directory layout
○ Especially if mixed with java
Kotlin IDEs
● You can write Kotlin next to Java in
your favorite IDE
● Kotlin works with JDK 1.6+
● IntelliJ IDEA (and Android Studio)
support Kotlin out of the Box
● There is a plugin for Eclipse too
Hello World
● Kotlin files have .kt extension
● You can also try your code in
REPL (Read-Eval-Print-Loop)
Let’s Mix with some Java
Java from Kotlin
● You can call Java code from you Kotlin files transparently
Convert Java to Kotlin
Code size
● According to Jetbrains converting a Java application to Kotlin is expected
to reduce the line count by 40%
○ More concise language (eg. Kotlin data classes can replace 50 line
classes with a single line)
○ The Kotlin standard library enhances existing Java classes with
extensions that trivialize common usage (eg. collections)
○ Kotlin allows you to extract more re-usable patterns than what Java
allows
Utility
● Top-level computed property
● String extension
Kotlin from Java
● In most cases the
integration is Seamless
● Some Kotlin features do
not exist in Java (e.g. top
level functions or
properties)
● In such cases conventions
are used
● In our case a static class is
generated for the top-level
declarations
Libraries & Resources
● You can use Any Java Library since Java and Kotlin are 100% interoperable
● Kotlin libraries: a nice curated list at https://kotlin.link
● Kotlin popularity is growing and resources become more abundant
Kotlin also lives outside the JVM
● Kotlin 1.1 (March 2017): officially
released the JavaScript target,
allowing you to compile Kotlin code
to JS and to run it in your browser
● Kotlin 1.2 (November 2017): adding
the possibility to reuse code
between the JVM and JavaScript
● Kotlin/Native v0.6 (Valentine’s Day
release 2018): Better support for
native targets (e.g. iOS,
WebAssembly, Windows)
Any Disadvantages?
● An app built with Kotlin will likely result in a larger file package size than one
built purely in Java
● The build time for Kotlin is a little slower
Final Thoughts
● The learning curve
○ IMHO comparatively small
● Not so popular yet
○ 44th in TIOBE Index for
February but competed
with C for language of the
year 2017
● Development Stability
○ Tools still in Beta
○ Static Analysis Tools
● Reversibility
○ Once you Go Kotlin…
[REF: Pinterest Engineering]
I am HERE!
IMHO Kotlin is here to stay
Questions?
Thank you!
http://antonis.me/

Contenu connexe

Tendances

Programming with Kotlin
Programming with KotlinProgramming with Kotlin
Programming with KotlinDavid Gassner
 
Voxxed Athens 2018 - Why Kotlin?
Voxxed Athens 2018 - Why Kotlin?Voxxed Athens 2018 - Why Kotlin?
Voxxed Athens 2018 - Why Kotlin?Voxxed Athens
 
Introduction to Kotlin for Java developer
Introduction to Kotlin for Java developerIntroduction to Kotlin for Java developer
Introduction to Kotlin for Java developerShuhei Shogen
 
Kotlin & arrow: the functional way
Kotlin & arrow:  the functional wayKotlin & arrow:  the functional way
Kotlin & arrow: the functional waynluaces
 
Kotlin Language powerpoint show file
Kotlin Language powerpoint show fileKotlin Language powerpoint show file
Kotlin Language powerpoint show fileSaurabh Tripathi
 
TDC2016SP - Groovy como você nunca viu
TDC2016SP - Groovy como você nunca viuTDC2016SP - Groovy como você nunca viu
TDC2016SP - Groovy como você nunca viutdc-globalcode
 
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
 
Introduction to Kotlin coroutines
Introduction to Kotlin coroutinesIntroduction to Kotlin coroutines
Introduction to Kotlin coroutinesRoman Elizarov
 
Kotlin for Android Development
Kotlin for Android DevelopmentKotlin for Android Development
Kotlin for Android DevelopmentSpeck&Tech
 
Devoxx 2013 Summary/Keynotes
Devoxx 2013 Summary/KeynotesDevoxx 2013 Summary/Keynotes
Devoxx 2013 Summary/KeynotesAliaksandr Kazlou
 
Introduction to Kotlin JVM language
Introduction to Kotlin JVM languageIntroduction to Kotlin JVM language
Introduction to Kotlin JVM languageAndrius Klimavicius
 
Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I Atif AbbAsi
 
Scaladays 2010 - The Scala IDE for Eclipse - Retrospect and Prospect for 2.8 ...
Scaladays 2010 - The Scala IDE for Eclipse - Retrospect and Prospect for 2.8 ...Scaladays 2010 - The Scala IDE for Eclipse - Retrospect and Prospect for 2.8 ...
Scaladays 2010 - The Scala IDE for Eclipse - Retrospect and Prospect for 2.8 ...Miles Sabin
 

Tendances (20)

Programming with Kotlin
Programming with KotlinProgramming with Kotlin
Programming with Kotlin
 
Voxxed Athens 2018 - Why Kotlin?
Voxxed Athens 2018 - Why Kotlin?Voxxed Athens 2018 - Why Kotlin?
Voxxed Athens 2018 - Why Kotlin?
 
Introduction to Kotlin for Java developer
Introduction to Kotlin for Java developerIntroduction to Kotlin for Java developer
Introduction to Kotlin for Java developer
 
Intro to kotlin
Intro to kotlinIntro to kotlin
Intro to kotlin
 
Kotlin & arrow: the functional way
Kotlin & arrow:  the functional wayKotlin & arrow:  the functional way
Kotlin & arrow: the functional way
 
Clojure presentation
Clojure presentationClojure presentation
Clojure presentation
 
Android with kotlin course
Android with kotlin courseAndroid with kotlin course
Android with kotlin course
 
Kotlin Language powerpoint show file
Kotlin Language powerpoint show fileKotlin Language powerpoint show file
Kotlin Language powerpoint show file
 
TDC2016SP - Groovy como você nunca viu
TDC2016SP - Groovy como você nunca viuTDC2016SP - Groovy como você nunca viu
TDC2016SP - Groovy como você nunca viu
 
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
 
Introduction to Kotlin coroutines
Introduction to Kotlin coroutinesIntroduction to Kotlin coroutines
Introduction to Kotlin coroutines
 
Kotlin for Android Development
Kotlin for Android DevelopmentKotlin for Android Development
Kotlin for Android Development
 
Coding in kotlin
Coding in kotlinCoding in kotlin
Coding in kotlin
 
Protocol buffers
Protocol buffersProtocol buffers
Protocol buffers
 
Kotlin from-scratch
Kotlin from-scratchKotlin from-scratch
Kotlin from-scratch
 
Devoxx 2013 Summary/Keynotes
Devoxx 2013 Summary/KeynotesDevoxx 2013 Summary/Keynotes
Devoxx 2013 Summary/Keynotes
 
Introduction to Kotlin JVM language
Introduction to Kotlin JVM languageIntroduction to Kotlin JVM language
Introduction to Kotlin JVM language
 
Four Python Pains
Four Python PainsFour Python Pains
Four Python Pains
 
Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I
 
Scaladays 2010 - The Scala IDE for Eclipse - Retrospect and Prospect for 2.8 ...
Scaladays 2010 - The Scala IDE for Eclipse - Retrospect and Prospect for 2.8 ...Scaladays 2010 - The Scala IDE for Eclipse - Retrospect and Prospect for 2.8 ...
Scaladays 2010 - The Scala IDE for Eclipse - Retrospect and Prospect for 2.8 ...
 

Similaire à A short introduction to the Kotlin language for Java developers

Kotlin - A Programming Language
Kotlin - A Programming Language Kotlin - A Programming Language
Kotlin - A Programming Language Mobio Solutions
 
Kotlin for Android
Kotlin for AndroidKotlin for Android
Kotlin for AndroidHan Yin
 
Say Goodbye To Java: Getting Started With Kotlin For Android Development
Say Goodbye To Java: Getting Started With Kotlin For Android DevelopmentSay Goodbye To Java: Getting Started With Kotlin For Android Development
Say Goodbye To Java: Getting Started With Kotlin For Android DevelopmentAdam Magaña
 
Introduction_to_Kotlin.pptx
Introduction_to_Kotlin.pptxIntroduction_to_Kotlin.pptx
Introduction_to_Kotlin.pptxHARSHITJAIN068
 
Kotlin & Arrow the functional way
Kotlin & Arrow the functional wayKotlin & Arrow the functional way
Kotlin & Arrow the functional wayThoughtworks
 
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
 
Developers’ mDay u Banjoj Luci - Duško Bajić, Kotlin User Group Bosnia – Kotl...
Developers’ mDay u Banjoj Luci - Duško Bajić, Kotlin User Group Bosnia – Kotl...Developers’ mDay u Banjoj Luci - Duško Bajić, Kotlin User Group Bosnia – Kotl...
Developers’ mDay u Banjoj Luci - Duško Bajić, Kotlin User Group Bosnia – Kotl...mCloud
 
Kotlin vs Java-A Comparison for Application Development Projects.pdf
Kotlin vs Java-A Comparison for Application Development Projects.pdfKotlin vs Java-A Comparison for Application Development Projects.pdf
Kotlin vs Java-A Comparison for Application Development Projects.pdfTechugo
 
Kotlin top features overview
Kotlin top features overviewKotlin top features overview
Kotlin top features overviewDennis Boanini
 
Counterclockwise past present future
Counterclockwise  past present futureCounterclockwise  past present future
Counterclockwise past present futurelolopetit
 
DSL's with Groovy
DSL's with GroovyDSL's with Groovy
DSL's with Groovypaulbowler
 
Kotlin Native - C / Swift Interop - ACCU Autmn 2019
Kotlin Native - C / Swift Interop - ACCU Autmn 2019Kotlin Native - C / Swift Interop - ACCU Autmn 2019
Kotlin Native - C / Swift Interop - ACCU Autmn 2019Eamonn Boyle
 
How to keep maintainability of long life Scala applications
How to keep maintainability of long life Scala applicationsHow to keep maintainability of long life Scala applications
How to keep maintainability of long life Scala applicationstakezoe
 

Similaire à A short introduction to the Kotlin language for Java developers (20)

Kotlin - A Programming Language
Kotlin - A Programming Language Kotlin - A Programming Language
Kotlin - A Programming Language
 
Why Kotlin?
Why Kotlin?Why Kotlin?
Why Kotlin?
 
Kotlin for Android
Kotlin for AndroidKotlin for Android
Kotlin for Android
 
Kotlin
KotlinKotlin
Kotlin
 
Kotlin
KotlinKotlin
Kotlin
 
Say Goodbye To Java: Getting Started With Kotlin For Android Development
Say Goodbye To Java: Getting Started With Kotlin For Android DevelopmentSay Goodbye To Java: Getting Started With Kotlin For Android Development
Say Goodbye To Java: Getting Started With Kotlin For Android Development
 
Kotlin Online Training.pdf
Kotlin Online Training.pdfKotlin Online Training.pdf
Kotlin Online Training.pdf
 
Introduction_to_Kotlin.pptx
Introduction_to_Kotlin.pptxIntroduction_to_Kotlin.pptx
Introduction_to_Kotlin.pptx
 
Kotlin & Arrow the functional way
Kotlin & Arrow the functional wayKotlin & Arrow the functional way
Kotlin & Arrow the functional way
 
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?
 
Introduction to Kotlin
Introduction to KotlinIntroduction to Kotlin
Introduction to Kotlin
 
Developers’ mDay u Banjoj Luci - Duško Bajić, Kotlin User Group Bosnia – Kotl...
Developers’ mDay u Banjoj Luci - Duško Bajić, Kotlin User Group Bosnia – Kotl...Developers’ mDay u Banjoj Luci - Duško Bajić, Kotlin User Group Bosnia – Kotl...
Developers’ mDay u Banjoj Luci - Duško Bajić, Kotlin User Group Bosnia – Kotl...
 
Kotlin vs Java-A Comparison for Application Development Projects.pdf
Kotlin vs Java-A Comparison for Application Development Projects.pdfKotlin vs Java-A Comparison for Application Development Projects.pdf
Kotlin vs Java-A Comparison for Application Development Projects.pdf
 
Kotlin top features overview
Kotlin top features overviewKotlin top features overview
Kotlin top features overview
 
Slides kotlin yajug
Slides kotlin yajugSlides kotlin yajug
Slides kotlin yajug
 
Kotlin Presentation
Kotlin PresentationKotlin Presentation
Kotlin Presentation
 
Counterclockwise past present future
Counterclockwise  past present futureCounterclockwise  past present future
Counterclockwise past present future
 
DSL's with Groovy
DSL's with GroovyDSL's with Groovy
DSL's with Groovy
 
Kotlin Native - C / Swift Interop - ACCU Autmn 2019
Kotlin Native - C / Swift Interop - ACCU Autmn 2019Kotlin Native - C / Swift Interop - ACCU Autmn 2019
Kotlin Native - C / Swift Interop - ACCU Autmn 2019
 
How to keep maintainability of long life Scala applications
How to keep maintainability of long life Scala applicationsHow to keep maintainability of long life Scala applications
How to keep maintainability of long life Scala applications
 

Dernier

%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...chiefasafspells
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 

Dernier (20)

%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 

A short introduction to the Kotlin language for Java developers

  • 1. for Java developers A short introduction to the Kotlin language Antonis Lilis Mobile Engineer @ JHUG Meetup February 2018
  • 2. Some History ● 2011: JetBrains unveiled Project Kotlin, a new language for the JVM ● 2012: JetBrains open sourced the project under the Apache 2 license ● 2016: Kotlin v1.0 is released ● 2017: Google announced first-class support for Kotlin on Android ● Kotlin is technically 7, but in reality 2 years old Trivia: The name comes from a small island in the Baltic Sea, near St.Petersburg. The language team decided to name it after an island just like Java (though Java was perhaps named after the coffee)
  • 3. The Kotlin Language ● Statically Typed ○ Type validation at compile time ● Supports Type Inference ○ Type automatically determined from the context ● Both Object Oriented and Functional ● First-class functions ○ You can store them in variables, pass them as parameters, or return them from other functions ● Was designed with Java Interoperability in mind
  • 4. Constants and Variables ● val (from value) ○ Immutable reference ● var (from variable) ○ Mutable reference ● Nullable Types ○ Defined Explicitly No semicolon here ;)
  • 5. Control Flow ● Classic loops: ○ if ○ for ○ while / do-while ● when ○ Replaces the switch operator ○ No breaks, no errors
  • 6. Functions ● Named arguments ● Can be declared at the top level of a file (without belonging to a class) ● Can be Nested ● Can have a block or expression body
  • 7. Functions ● Default parameter values ○ Avoids method overloading and boilerplate code Simple string Interpolation
  • 8. Classes data classes: autogenerated implementations of universal methods (equals, hashCode etc) “Any” is the analogue of java Object: a superclass of all classes
  • 9. Properties ● first-class language feature ● combination of the field and its accessors
  • 10. Modifiers ● Access modifiers ○ final (default) ○ open ○ abstract ● Visibility modifiers ○ public (default) ○ internal ○ protected ○ private "Design and document for inheritance or else prohibit it" Joshua J. Bloch, Effective Java ps. Lukas Lechner has written a series of articles on "How Effective Java influenced Kotlin" (http://lukle.at)
  • 11. No static keyword ● Top-level functions and properties (e.g. for utility classes) ● Companion objects ● The object keyword: declaring a class and creating an instance combined (Singleton)
  • 12. Extensions ● Enable adding methods and properties to other people’s classes ○ Of Course without access to private or protected members of the class
  • 13. Null Checks ● Safe-call operator ?. ● Elvis operator ?: ● The let function "I call it my billion-dollar mistake. It was the invention of the null reference in 1965" Tony Hoare
  • 15. Safe Casting ● Safe cast operator as? ● Smart cast ○ combining type checks and casts
  • 16. Collections ● Kotlin enhances the Java collection classes (List, Set, Map) Chained Calls
  • 17. Delegation ● Composition over Inheritance design pattern ● Native support for delegation (implicit delegation) ● Zero Boilerplate code ● Supports both Class Delegation and Delegated Properties Class Car inherits from an interface Nameable and delegates all of its public methods to a delegate object defined with the by keyword
  • 18. Lamdas and Higher Order Functions
  • 19. Domain-specific language construction ● Kotlin provides mechanisms for creating internal DSLs that use exactly the same syntax as all language features and are fully statically typed
  • 20. Coroutines ● Introduced in Kotlin 1.1 (March 2017) ● A way to write asynchronous code sequentially ● Multithreading in a way that is easily debuggable and maintainable ● Based on the idea of suspending function execution ● More lightweight and efficient than threads
  • 21. Source Code Layout ● Packages (similar to that in Java) ● Multiple classes can fit in the same file ● You can choose any name for files (not restricted to class name) ● The import keyword is not restricted to importing classes ○ Top-level functions and properties can be imported ● Kotlin does not impose any restrictions on the layout of source files on disk ● Good practice to follow Java’s directory layout ○ Especially if mixed with java
  • 22.
  • 23. Kotlin IDEs ● You can write Kotlin next to Java in your favorite IDE ● Kotlin works with JDK 1.6+ ● IntelliJ IDEA (and Android Studio) support Kotlin out of the Box ● There is a plugin for Eclipse too
  • 24. Hello World ● Kotlin files have .kt extension ● You can also try your code in REPL (Read-Eval-Print-Loop)
  • 25. Let’s Mix with some Java
  • 26. Java from Kotlin ● You can call Java code from you Kotlin files transparently
  • 27. Convert Java to Kotlin
  • 28. Code size ● According to Jetbrains converting a Java application to Kotlin is expected to reduce the line count by 40% ○ More concise language (eg. Kotlin data classes can replace 50 line classes with a single line) ○ The Kotlin standard library enhances existing Java classes with extensions that trivialize common usage (eg. collections) ○ Kotlin allows you to extract more re-usable patterns than what Java allows
  • 29. Utility ● Top-level computed property ● String extension
  • 30. Kotlin from Java ● In most cases the integration is Seamless ● Some Kotlin features do not exist in Java (e.g. top level functions or properties) ● In such cases conventions are used ● In our case a static class is generated for the top-level declarations
  • 31. Libraries & Resources ● You can use Any Java Library since Java and Kotlin are 100% interoperable ● Kotlin libraries: a nice curated list at https://kotlin.link ● Kotlin popularity is growing and resources become more abundant
  • 32. Kotlin also lives outside the JVM ● Kotlin 1.1 (March 2017): officially released the JavaScript target, allowing you to compile Kotlin code to JS and to run it in your browser ● Kotlin 1.2 (November 2017): adding the possibility to reuse code between the JVM and JavaScript ● Kotlin/Native v0.6 (Valentine’s Day release 2018): Better support for native targets (e.g. iOS, WebAssembly, Windows)
  • 33. Any Disadvantages? ● An app built with Kotlin will likely result in a larger file package size than one built purely in Java ● The build time for Kotlin is a little slower
  • 34. Final Thoughts ● The learning curve ○ IMHO comparatively small ● Not so popular yet ○ 44th in TIOBE Index for February but competed with C for language of the year 2017 ● Development Stability ○ Tools still in Beta ○ Static Analysis Tools ● Reversibility ○ Once you Go Kotlin… [REF: Pinterest Engineering] I am HERE! IMHO Kotlin is here to stay