A quick and fast intro to Kotlin
KOTLIN
● Primary target JVM
● Javascript
● Compiled in Java byte code
● Created for industry
Core goals is 100% Java interoperability.
KOTLIN main features
Concise
Drastically reduce the amount of
boilerplate code you need to write.
Safe
Avoid entire classes of errors such
as null pointer exceptions.
Interoperable
Leverage existing frameworks and
libraries of the JVM with 100% Java
Interoperability.
data class Person(
var name: String,
var surname: String,
var age: Int)
Create a POJO with:
● Getters
● Setters
● equals()
● hashCode()
● toString()
● copy()
Concise
public class Person {
final String firstName;
final String lastName;
public Person(...) {
...
}
// Getters
...
// Hashcode / equals
...
// Tostring
...
// Egh...
}
KOTLIN lambdas
● must always appear between curly brackets
● if there is a single parameter then it can be referred to by it
Concise
val list = (0..49).toList()
val filtered = list
.filter({ x -> x % 2 == 0 })
val list = (0..49).toList()
val filtered = list
.filter { it % 2 == 0 }
NULL safety
// ERROR
// OK
// OK
// ERROR
NULL safety
// ERROR
NULL safety
// ERROR
SAFE CALL
NULL safety
Extend existing classes functionality
Ability to extend a class with new functionality without having to inherit from the class
● does not modify classes
● are resolved statically
Extend existing classes functionality
fun String.lastChar() = this.charAt(this.length() - 1)
// this can be omitted
fun String.lastChar() = charAt(length() - 1)
fun use(){
Val c: Char = "abc".lastChar()
}
Everything is an expression
val max = if (a > b) a else b
val hasPrefix = when(x) {
is String -> x.startsWith("prefix")
else -> false
}
when(x) {
in 1..10 -> ...
102 -> ...
else -> ...
}
boolean hasPrefix;
if (x instanceof String)
hasPrefix = x.startsWith("prefix");
else
hasPrefix = false;
switch (month) {
case 1: ... break
case 7: ... break
default: ...
}
Default Parameters
fun foo( i :Int, s: String = "", b: Boolean = true) {}
fun usage(){
foo( 1, b = false)
}
for loop
can iterate over any type that provides an iterator implementing next() and hasNext()
for (item in collection)
print(item)
for ((index, value) in array.withIndex()) {
println("the element at $index is $value")
}
Collections
● Made easy
● distinguishes between immutable and mutable collections
val numbers: MutableList = mutableListOf(1, 2, 3)
val readOnlyNumbers: List = numbers
numbers.add(4)
println(readOnlyView) // prints "[1, 2, 3, 4]"
readOnlyNumbers.clear() // -> does not compile
Java 6
// Using R.layout.activity_main from the main source set
import kotlinx.android.synthetic.main.activity_main.*
class MyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView.setText("Hello, world!")
}
}
public class MyActivity extends Activity() {
@override
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("Hello, world!");
}
}
Kotlin Android Extensions
Extension functions
fun Fragment.toast(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(getActivity(), message, duration).show()
}
fragment.toast("Hello world!")
Activities
startActivity( intentFor< NewActivity > ("Answer" to 42) )
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("Answer", 42);
startActivity(intent);
Functional support (Lambdas)
view.setOnClickListener { toast("Hello world!") }
View view = (View) findViewById(R.id.view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(this, "asdf", Toast.LENGTH_LONG)
.show();
}
});
Dynamic Layout
scrollView {
linearLayout(LinearLayout.VERTICAL) {
val label = textView("?")
button("Click me!") {
label.setText("Clicked!")
}
editText("Edit me!")
// codice koltin
// ...
}
}.style(...)
?
Click me!
Edit me!
Easily mixed with Java
● Do not have to convert everything at once
● You can convert little portions
● Write kotlin code over the existing Java code
Everything still works
Kotlin costs nothing to adopt
● It’s open source
● There’s a high quality, one-click Java to Kotlin converter tool
● Can use all existing Java frameworks and libraries
● It integrates with Maven, Gradle and other build systems.
● Great for Android, compiles for java 6 byte code
● Very small runtime library 924 KB
Kotlin usefull links
● A very well done documentation : Tutorial, Videos
● Kotlin Koans online
● Constantly updating in Github, kotlin-projects
● Talks: Kotlin in Action, Kotlin on Android
What Java has that Kotlin does not
https://kotlinlang.org/docs/reference/comparison-to-java.html
Primitive Types
Everything is an object
we can call member functions and properties on any variable.
What Java has that Kotlin does not
val a: Int? = 1
val b: Long? = a
print(a == b)
Primitive Types
Everything is an object
we can call member functions and properties on any variable.
What Java has that Kotlin does not
val a: Int? = 1
val b: Long? = a
print(a == b) // FALSE //
Primitive Types
Everything is an object
we can call member functions and properties on any variable.
What Java has that Kotlin does not
val b: Byte = 1
val i: Int = b
val i: Int = b.toInt()
val a: Int? = 1
val b: Long? = a
print(a == b) // FALSE //
Primitive Types
Everything is an object
we can call member functions and properties on any variable.
What Java has that Kotlin does not
val b: Byte = 1
val i: Int = b // ERROR //
val i: Int = b.toInt() // OK //
val a: Int? = 1
val b: Long? = a
print(a == b) // FALSE //
Static Members
class MyClass {
companion object Factory {
fun create(): MyClass = MyClass()
}
}
val instance = MyClass.create()
What Java has that Kotlin does not
Singleton
object MyClass {
// ....
}
Gradle Goes Kotlin
https://kotlinlang.org/docs/reference/using-gradle.html
Thank You
Erinda Jaupaj
@ErindaJaupi

More Related Content

PPTX
Introduction to Koltin for Android Part I
PDF
Kotlin for Android Development
PPT
The Kotlin Programming Language
PPTX
Introduction to Kotlin Language and its application to Android platform
PPTX
Intro to kotlin
PDF
Introduction to kotlin
PDF
Introduction to kotlin for android app development gdg ahmedabad dev fest 2017
PPTX
Kotlin as a Better Java
Introduction to Koltin for Android Part I
Kotlin for Android Development
The Kotlin Programming Language
Introduction to Kotlin Language and its application to Android platform
Intro to kotlin
Introduction to kotlin
Introduction to kotlin for android app development gdg ahmedabad dev fest 2017
Kotlin as a Better Java

What's hot (20)

PPTX
Introduction to kotlin and OOP in Kotlin
PPTX
Kotlin InDepth Tutorial for beginners 2022
PDF
Android Jetpack Compose - Turkey 2021
PDF
Kotlin vs Java | Edureka
PPTX
Kotlin Jetpack Tutorial
PPTX
Kotlin on android
PPT
SQLITE Android
PPTX
Java 8 - Features Overview
PPTX
Kotlin Collections
PPTX
SQLite database in android
PPTX
java 8 new features
PPTX
Jetpack Compose.pptx
PDF
What is REST API? REST API Concepts and Examples | Edureka
PPT
Core java concepts
PDF
Collections In Java
PPT
PDF
Java 8 features
PDF
Understanding react hooks
PDF
Declarative UIs with Jetpack Compose
Introduction to kotlin and OOP in Kotlin
Kotlin InDepth Tutorial for beginners 2022
Android Jetpack Compose - Turkey 2021
Kotlin vs Java | Edureka
Kotlin Jetpack Tutorial
Kotlin on android
SQLITE Android
Java 8 - Features Overview
Kotlin Collections
SQLite database in android
java 8 new features
Jetpack Compose.pptx
What is REST API? REST API Concepts and Examples | Edureka
Core java concepts
Collections In Java
Java 8 features
Understanding react hooks
Declarative UIs with Jetpack Compose
Ad

Similar to A quick and fast intro to Kotlin (20)

PDF
Kotlin, smarter development for the jvm
PDF
Little Helpers for Android Development with Kotlin
PPTX
Kotlin – the future of android
PDF
Having Fun with Kotlin Android - DILo Surabaya
PPTX
K is for Kotlin
PPTX
Why kotlininandroid
PDF
Be More Productive with Kotlin
PDF
Develop your next app with kotlin @ AndroidMakersFr 2017
PDF
Kotlin: a better Java
PDF
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
PDF
Intro to Kotlin
PDF
Kotlin: A pragmatic language by JetBrains
PDF
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
PDF
Kotlin Developer Starter in Android projects
PPTX
PDF
Kotlin: forse è la volta buona (Trento)
PDF
Kotlin for Android Developers - 1
PDF
Kotlin @ Devoxx 2011
PDF
Kotlin Slides from Devoxx 2011
PDF
ADG Poznań - Kotlin for Android developers
Kotlin, smarter development for the jvm
Little Helpers for Android Development with Kotlin
Kotlin – the future of android
Having Fun with Kotlin Android - DILo Surabaya
K is for Kotlin
Why kotlininandroid
Be More Productive with Kotlin
Develop your next app with kotlin @ AndroidMakersFr 2017
Kotlin: a better Java
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Intro to Kotlin
Kotlin: A pragmatic language by JetBrains
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android projects
Kotlin: forse è la volta buona (Trento)
Kotlin for Android Developers - 1
Kotlin @ Devoxx 2011
Kotlin Slides from Devoxx 2011
ADG Poznań - Kotlin for Android developers
Ad

More from XPeppers (20)

PDF
Yagni, You aren't gonna need it
PDF
Jenkins Shared Libraries
PDF
The Continuous Delivery process
PDF
How Agile Dev Teams work
PDF
The Phoenix Project: un romanzo sull'IT
PDF
Metriche per finanziare il cambiamento
PDF
How do you handle renaming of a resource in RESTful way
PDF
La tecnica del pomodoro - Come viene adottata in XPeppers
PDF
Collective code ownership in Extreme Programming
PDF
What is Agile?
PDF
Improve your TDD skills
PDF
Test driven infrastructure
PDF
Banche agili un ossimoro?
PDF
Hiring Great People: how we improved our recruiting process to build and grow...
PDF
Continuous Delivery in Java
PDF
Life in XPeppers
PDF
Cloud e innovazione
PDF
Company culture slides
PDF
Agileday2013 Bravi si diventa
PDF
Agileday2013 pratiche agili applicate all'infrastruttura
Yagni, You aren't gonna need it
Jenkins Shared Libraries
The Continuous Delivery process
How Agile Dev Teams work
The Phoenix Project: un romanzo sull'IT
Metriche per finanziare il cambiamento
How do you handle renaming of a resource in RESTful way
La tecnica del pomodoro - Come viene adottata in XPeppers
Collective code ownership in Extreme Programming
What is Agile?
Improve your TDD skills
Test driven infrastructure
Banche agili un ossimoro?
Hiring Great People: how we improved our recruiting process to build and grow...
Continuous Delivery in Java
Life in XPeppers
Cloud e innovazione
Company culture slides
Agileday2013 Bravi si diventa
Agileday2013 pratiche agili applicate all'infrastruttura

Recently uploaded (20)

PDF
giants, standing on the shoulders of - by Daniel Stenberg
PDF
Dell Pro Micro: Speed customer interactions, patient processing, and learning...
PDF
Altius execution marketplace concept.pdf
PDF
Examining Bias in AI Generated News Content.pdf
PDF
5-Ways-AI-is-Revolutionizing-Telecom-Quality-Engineering.pdf
PDF
AI.gov: A Trojan Horse in the Age of Artificial Intelligence
PDF
4 layer Arch & Reference Arch of IoT.pdf
PDF
Transform-Quality-Engineering-with-AI-A-60-Day-Blueprint-for-Digital-Success.pdf
PDF
“The Future of Visual AI: Efficient Multimodal Intelligence,” a Keynote Prese...
PDF
A symptom-driven medical diagnosis support model based on machine learning te...
PDF
Early detection and classification of bone marrow changes in lumbar vertebrae...
PDF
Data Virtualization in Action: Scaling APIs and Apps with FME
PDF
Co-training pseudo-labeling for text classification with support vector machi...
PDF
SaaS reusability assessment using machine learning techniques
PPTX
Build automations faster and more reliably with UiPath ScreenPlay
PDF
CEH Module 2 Footprinting CEH V13, concepts
PDF
The-Future-of-Automotive-Quality-is-Here-AI-Driven-Engineering.pdf
PDF
Advancing precision in air quality forecasting through machine learning integ...
PDF
substrate PowerPoint Presentation basic one
PDF
MENA-ECEONOMIC-CONTEXT-VC MENA-ECEONOMIC
giants, standing on the shoulders of - by Daniel Stenberg
Dell Pro Micro: Speed customer interactions, patient processing, and learning...
Altius execution marketplace concept.pdf
Examining Bias in AI Generated News Content.pdf
5-Ways-AI-is-Revolutionizing-Telecom-Quality-Engineering.pdf
AI.gov: A Trojan Horse in the Age of Artificial Intelligence
4 layer Arch & Reference Arch of IoT.pdf
Transform-Quality-Engineering-with-AI-A-60-Day-Blueprint-for-Digital-Success.pdf
“The Future of Visual AI: Efficient Multimodal Intelligence,” a Keynote Prese...
A symptom-driven medical diagnosis support model based on machine learning te...
Early detection and classification of bone marrow changes in lumbar vertebrae...
Data Virtualization in Action: Scaling APIs and Apps with FME
Co-training pseudo-labeling for text classification with support vector machi...
SaaS reusability assessment using machine learning techniques
Build automations faster and more reliably with UiPath ScreenPlay
CEH Module 2 Footprinting CEH V13, concepts
The-Future-of-Automotive-Quality-is-Here-AI-Driven-Engineering.pdf
Advancing precision in air quality forecasting through machine learning integ...
substrate PowerPoint Presentation basic one
MENA-ECEONOMIC-CONTEXT-VC MENA-ECEONOMIC

A quick and fast intro to Kotlin

  • 2. KOTLIN ● Primary target JVM ● Javascript ● Compiled in Java byte code ● Created for industry Core goals is 100% Java interoperability.
  • 3. KOTLIN main features Concise Drastically reduce the amount of boilerplate code you need to write. Safe Avoid entire classes of errors such as null pointer exceptions. Interoperable Leverage existing frameworks and libraries of the JVM with 100% Java Interoperability.
  • 4. data class Person( var name: String, var surname: String, var age: Int) Create a POJO with: ● Getters ● Setters ● equals() ● hashCode() ● toString() ● copy() Concise public class Person { final String firstName; final String lastName; public Person(...) { ... } // Getters ... // Hashcode / equals ... // Tostring ... // Egh... }
  • 5. KOTLIN lambdas ● must always appear between curly brackets ● if there is a single parameter then it can be referred to by it Concise val list = (0..49).toList() val filtered = list .filter({ x -> x % 2 == 0 }) val list = (0..49).toList() val filtered = list .filter { it % 2 == 0 }
  • 10. Extend existing classes functionality Ability to extend a class with new functionality without having to inherit from the class ● does not modify classes ● are resolved statically
  • 11. Extend existing classes functionality fun String.lastChar() = this.charAt(this.length() - 1) // this can be omitted fun String.lastChar() = charAt(length() - 1) fun use(){ Val c: Char = "abc".lastChar() }
  • 12. Everything is an expression val max = if (a > b) a else b val hasPrefix = when(x) { is String -> x.startsWith("prefix") else -> false } when(x) { in 1..10 -> ... 102 -> ... else -> ... } boolean hasPrefix; if (x instanceof String) hasPrefix = x.startsWith("prefix"); else hasPrefix = false; switch (month) { case 1: ... break case 7: ... break default: ... }
  • 13. Default Parameters fun foo( i :Int, s: String = "", b: Boolean = true) {} fun usage(){ foo( 1, b = false) }
  • 14. for loop can iterate over any type that provides an iterator implementing next() and hasNext() for (item in collection) print(item) for ((index, value) in array.withIndex()) { println("the element at $index is $value") }
  • 15. Collections ● Made easy ● distinguishes between immutable and mutable collections val numbers: MutableList = mutableListOf(1, 2, 3) val readOnlyNumbers: List = numbers numbers.add(4) println(readOnlyView) // prints "[1, 2, 3, 4]" readOnlyNumbers.clear() // -> does not compile
  • 17. // Using R.layout.activity_main from the main source set import kotlinx.android.synthetic.main.activity_main.* class MyActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) textView.setText("Hello, world!") } } public class MyActivity extends Activity() { @override void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = (TextView) findViewById(R.id.textView); textView.setText("Hello, world!"); } } Kotlin Android Extensions
  • 18. Extension functions fun Fragment.toast(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) { Toast.makeText(getActivity(), message, duration).show() } fragment.toast("Hello world!")
  • 19. Activities startActivity( intentFor< NewActivity > ("Answer" to 42) ) Intent intent = new Intent(this, NewActivity.class); intent.putExtra("Answer", 42); startActivity(intent);
  • 20. Functional support (Lambdas) view.setOnClickListener { toast("Hello world!") } View view = (View) findViewById(R.id.view); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(this, "asdf", Toast.LENGTH_LONG) .show(); } });
  • 21. Dynamic Layout scrollView { linearLayout(LinearLayout.VERTICAL) { val label = textView("?") button("Click me!") { label.setText("Clicked!") } editText("Edit me!") // codice koltin // ... } }.style(...) ? Click me! Edit me!
  • 22. Easily mixed with Java ● Do not have to convert everything at once ● You can convert little portions ● Write kotlin code over the existing Java code Everything still works
  • 23. Kotlin costs nothing to adopt ● It’s open source ● There’s a high quality, one-click Java to Kotlin converter tool ● Can use all existing Java frameworks and libraries ● It integrates with Maven, Gradle and other build systems. ● Great for Android, compiles for java 6 byte code ● Very small runtime library 924 KB
  • 24. Kotlin usefull links ● A very well done documentation : Tutorial, Videos ● Kotlin Koans online ● Constantly updating in Github, kotlin-projects ● Talks: Kotlin in Action, Kotlin on Android
  • 25. What Java has that Kotlin does not https://kotlinlang.org/docs/reference/comparison-to-java.html
  • 26. Primitive Types Everything is an object we can call member functions and properties on any variable. What Java has that Kotlin does not val a: Int? = 1 val b: Long? = a print(a == b)
  • 27. Primitive Types Everything is an object we can call member functions and properties on any variable. What Java has that Kotlin does not val a: Int? = 1 val b: Long? = a print(a == b) // FALSE //
  • 28. Primitive Types Everything is an object we can call member functions and properties on any variable. What Java has that Kotlin does not val b: Byte = 1 val i: Int = b val i: Int = b.toInt() val a: Int? = 1 val b: Long? = a print(a == b) // FALSE //
  • 29. Primitive Types Everything is an object we can call member functions and properties on any variable. What Java has that Kotlin does not val b: Byte = 1 val i: Int = b // ERROR // val i: Int = b.toInt() // OK // val a: Int? = 1 val b: Long? = a print(a == b) // FALSE //
  • 30. Static Members class MyClass { companion object Factory { fun create(): MyClass = MyClass() } } val instance = MyClass.create() What Java has that Kotlin does not Singleton object MyClass { // .... }