SlideShare a Scribd company logo
What’s new in
otlin
Vipul Asri
Overview
● Kotlin is a new programming language from
JetBrains
● First appeared in 2011, stable release 1.0 in Feb,
2016
● “Statically typed programming language”
● Procedural as well as Functional Programming
both
● Google announced official support for Kotlin on
Android at Google I/O, 2017
Benefits
● Kotlin compiles to JVM bytecode or JavaScript
● Kotlin programs can use all existing Java
Frameworks and Libraries
● Kotlin can be learned easily
● Automatic conversion of Java to Kotlin with
Android Studio
● Kotlin’s null-safety is great
Features
Extension Functions
fun String.removeSpaces() { … }
// call to extension function
“Hello, I am Vipul Asri”.removeSpaces()
// output
“Hello,IamVipulAsri”
Inter-operable With Java
Java code can be used inside Kotlin code and vice-versa.
class Person {
String name = “Person Nath”
public String getNameWithoutSpace() {
return name.removeSpaces();
}
}
Synthetic Extension( bye bye to findViewById() )
Java
class MainActivity extends AppCompatActivity{
TextView studentName;
@Override
protected void onCreate(Bundle
savedInstanceState) {
studentName =
findViewById(R.id.studentName);
studentName.setText("Harshit Dwivedi");
}
}
Kotlin
import
kotlinx.android.synthetic.main.activity_main.*
class KotlinActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState:
Bundle?) {
studentName.text = "Harshit" //That's it!
}
}
NPE Safety
var name_1: String = "abc"
name_1 = null // try to assign null, won’t compile.
// To allow nulls, we declare a variable as nullable string
var name_2: String? = "abc"
name_2 = null // no compilation error.
val len = name_1.length // there will be no NPE
val len = name_2.length // error: variable 'name_2' can be null
NPE Safety
1. Checking for null in conditions
val len = if (name_2 != null) name_2.length else -1
2. Safe Calls (with ?.)
name_2?.length // name_2.length if name_2 is not null, and null otherwise
3. The !! Operator (for NPE Lovers) - not-null assertion operator (!!)
val l = b!!.length // (!!) converts any value to a non-null type
// and throws an exception if the value is null
Coroutines (experimental)
● Threads can easily eat up almost a megabyte of memory.
In this context, we can call coroutines (as per the
documentation) as "lightweight" threads.
● A coroutine is sitting on an actual thread pool, which is
used for background execution.
● It only uses the resources when it needs it.
launch{}
val job = launch {
val userString = fetchUserString("1")
val user = deserializeUser(userString)
log(user.name)
}
The wrapped code is dispatched to a background thread, and
the function itself returns a Job instance, which can be used in
other coroutines to control the execution.
async{}
val user = async {
val userString = fetchUserString("1")
val user = deserializeUser(userString)
user
}.await()
The async function returns a Defered<T> instance, and calling
await() on it you can get the actual result of the computation.
Comparison
Java vs Kotlin
No semicolons
Java
System.out.println(“Hello”);
Kotlin
println(“Hello”)
Type Declaration
Java
int a = 1;
String s = “abc”;
boolean b;
Kotlin
val a: Int = 1
val s: String = “abc”
val b: Boolean
Type Inference
Java
int a = 1;
String s = “abc”;
boolean b;
Kotlin
val a = 1
val s = “abc”
val b: Boolean
Object Declaration - No ‘new’
Java
Car car = new Car();
Kotlin
val car = Car()
String templates
Java
String s = String.format(“%s
has %d apples, name,
count);
Kotlin
val s = “$name has $count
apples”
Functions
Java
String getName(Person person) {
return person.getName();
}
Kotlin
fun getName(person: Person): String {
return person.name
}
Expressions body
Java
int sum(int a , int b) {
return a+b;
}
Kotlin
val sum(a: Int , b:Int) = a+b
‘When’ Expression
Java
switch(x) {
case 1: log.info("x == 1"); break;
case 2: log.info("x == 2"); break;
default: log.info("x is neither 1 nor
2");
}
Kotlin
when (x) {
in 0,1 -> print("x is too low")
in 2..10 -> print("x is in range")
!in 10..20 -> print("x outside range")
parseInt(s) -> print("s encodes x")
is Program -> print("It's a program!")
else -> print("None of the above")
}
Ranges
Java
IntStream.rangeClosed(1, 5)
.forEach(x -> ...);
Kotlin
for (x in 1..5) { ... }
Data Classes
Java
class Address {
final String city;
Address(String n) { city = n; }
String getCity() { return city; }
void setCity(String n) { city = n; }
int equals() { ... }
hashCode() { ... }
toString() { ... }
copy() { ... } }
Kotlin
data class Address(var city:String,
var country:Country)
Kotlin 1.2
Sharing Code between
Platforms
● JavaScript target, allowing you to compile Kotlin code to JS and to run it in
your browser
● Reuse code between the JVM and JavaScript
● Business logic of your application once, and reuse it across all tiers of your
application – the backend, the browser frontend and the Android mobile app
What’s new in Kotlin?
Thank You!

More Related Content

PDF
Kotlin cheat sheet by ekito
PDF
Kotlin hands on - MorningTech ekito 2017
PDF
Kotlin - Better Java
PDF
Kotlin boost yourproductivity
PPTX
Fall in love with Kotlin
PDF
Introduction to kotlin for android app development gdg ahmedabad dev fest 2017
ODP
JavaScript global object, execution contexts & closures
PDF
JavaScript Execution Context
Kotlin cheat sheet by ekito
Kotlin hands on - MorningTech ekito 2017
Kotlin - Better Java
Kotlin boost yourproductivity
Fall in love with Kotlin
Introduction to kotlin for android app development gdg ahmedabad dev fest 2017
JavaScript global object, execution contexts & closures
JavaScript Execution Context

What's hot (20)

PDF
Clojure made-simple - John Stevenson
PDF
Taking Kotlin to production, Seriously
PPTX
Design patterns with kotlin
PDF
2017: Kotlin - now more than ever
PPTX
Introduction to Koltin for Android Part I
PPTX
Lecture 4.2 c++(comlete reference book)
PDF
Swift and Kotlin Presentation
PDF
Let'swift "Concurrency in swift"
PPTX
Say Goodbye To Java: Getting Started With Kotlin For Android Development
PDF
Kotlin for Android devs
PPT
Java 7
PPTX
Nice to meet Kotlin
PPTX
Getting Started With Kotlin
ODP
JavaScript Object Oriented Programming Cheat Sheet
PDF
Frege Tutorial at JavaOne 2015
PDF
Blocks & GCD
PDF
Kotlin workshop 2018-06-11
PDF
CODEsign 2015
PDF
ConFess Vienna 2015 - Metaprogramming with Groovy
Clojure made-simple - John Stevenson
Taking Kotlin to production, Seriously
Design patterns with kotlin
2017: Kotlin - now more than ever
Introduction to Koltin for Android Part I
Lecture 4.2 c++(comlete reference book)
Swift and Kotlin Presentation
Let'swift "Concurrency in swift"
Say Goodbye To Java: Getting Started With Kotlin For Android Development
Kotlin for Android devs
Java 7
Nice to meet Kotlin
Getting Started With Kotlin
JavaScript Object Oriented Programming Cheat Sheet
Frege Tutorial at JavaOne 2015
Blocks & GCD
Kotlin workshop 2018-06-11
CODEsign 2015
ConFess Vienna 2015 - Metaprogramming with Groovy
Ad

Similar to What’s new in Kotlin? (20)

PPTX
Why kotlininandroid
PPTX
Kotlin - A Programming Language
PPTX
Introduction to Kotlin Language and its application to Android platform
PPTX
PDF
Kotlin: A pragmatic language by JetBrains
PDF
A short introduction to the Kotlin language for Java developers
PDF
Kotlin, smarter development for the jvm
PDF
Lightning talk: Kotlin
PDF
JavaOne 2016 - Kotlin: The Language of The Future For JVM?
PPTX
Java vs kotlin
PPTX
Exploring Kotlin language basics for Android App development
PDF
Kotlin for Android Developers - 1
PDF
Is this Swift for Android? A short introduction to the Kotlin language
PDF
Be More Productive with Kotlin
PDF
Kotlin what_you_need_to_know-converted event 4 with nigerians
PDF
ADG Poznań - Kotlin for Android developers
PPTX
Kotlin – the future of android
PPTX
Kotlin
PDF
JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)
PDF
2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf
Why kotlininandroid
Kotlin - A Programming Language
Introduction to Kotlin Language and its application to Android platform
Kotlin: A pragmatic language by JetBrains
A short introduction to the Kotlin language for Java developers
Kotlin, smarter development for the jvm
Lightning talk: Kotlin
JavaOne 2016 - Kotlin: The Language of The Future For JVM?
Java vs kotlin
Exploring Kotlin language basics for Android App development
Kotlin for Android Developers - 1
Is this Swift for Android? A short introduction to the Kotlin language
Be More Productive with Kotlin
Kotlin what_you_need_to_know-converted event 4 with nigerians
ADG Poznań - Kotlin for Android developers
Kotlin – the future of android
Kotlin
JDD 2017: Kotlin for Java developers (Tomasz Kleszczyński)
2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf
Ad

More from Squareboat (20)

PDF
Squareboat Deck
PDF
Squareboat Branding Proposal
PDF
Squareboat Product Foundation Process
PDF
Squareboat Culture Deck
PDF
Squareboat Design Portfolio
PDF
Squareboat Crew Deck
PDF
High Performance Mysql - Friday Tech Talks at Squareboat
PDF
CTA - Call to Attention
PDF
Tech talk on Tailwind CSS
PDF
What’s New in Apple World - WWDC19
PDF
Tech Talk on Microservices at Squareboat
PDF
Building Alexa Skills
PDF
Making Products to get users “Hooked”
PDF
Moving to Docker... Finally!
PDF
Color Theory
PDF
Continuous Delivery process
PDF
HTML and CSS architecture for 2025
PDF
Vue JS
PDF
The rise of Conversational User Interfaces
PDF
Thinking of growth as a feature
Squareboat Deck
Squareboat Branding Proposal
Squareboat Product Foundation Process
Squareboat Culture Deck
Squareboat Design Portfolio
Squareboat Crew Deck
High Performance Mysql - Friday Tech Talks at Squareboat
CTA - Call to Attention
Tech talk on Tailwind CSS
What’s New in Apple World - WWDC19
Tech Talk on Microservices at Squareboat
Building Alexa Skills
Making Products to get users “Hooked”
Moving to Docker... Finally!
Color Theory
Continuous Delivery process
HTML and CSS architecture for 2025
Vue JS
The rise of Conversational User Interfaces
Thinking of growth as a feature

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Approach and Philosophy of On baking technology
PPTX
Big Data Technologies - Introduction.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Spectroscopy.pptx food analysis technology
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
KodekX | Application Modernization Development
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Unlocking AI with Model Context Protocol (MCP)
Reach Out and Touch Someone: Haptics and Empathic Computing
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
The AUB Centre for AI in Media Proposal.docx
Advanced methodologies resolving dimensionality complications for autism neur...
Approach and Philosophy of On baking technology
Big Data Technologies - Introduction.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Spectroscopy.pptx food analysis technology
Per capita expenditure prediction using model stacking based on satellite ima...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Empathic Computing: Creating Shared Understanding
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Building Integrated photovoltaic BIPV_UPV.pdf
KodekX | Application Modernization Development
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
cuic standard and advanced reporting.pdf
Electronic commerce courselecture one. Pdf
Network Security Unit 5.pdf for BCA BBA.
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows

What’s new in Kotlin?

  • 2. Overview ● Kotlin is a new programming language from JetBrains ● First appeared in 2011, stable release 1.0 in Feb, 2016 ● “Statically typed programming language” ● Procedural as well as Functional Programming both ● Google announced official support for Kotlin on Android at Google I/O, 2017
  • 3. Benefits ● Kotlin compiles to JVM bytecode or JavaScript ● Kotlin programs can use all existing Java Frameworks and Libraries ● Kotlin can be learned easily ● Automatic conversion of Java to Kotlin with Android Studio ● Kotlin’s null-safety is great
  • 5. Extension Functions fun String.removeSpaces() { … } // call to extension function “Hello, I am Vipul Asri”.removeSpaces() // output “Hello,IamVipulAsri”
  • 6. Inter-operable With Java Java code can be used inside Kotlin code and vice-versa. class Person { String name = “Person Nath” public String getNameWithoutSpace() { return name.removeSpaces(); } }
  • 7. Synthetic Extension( bye bye to findViewById() ) Java class MainActivity extends AppCompatActivity{ TextView studentName; @Override protected void onCreate(Bundle savedInstanceState) { studentName = findViewById(R.id.studentName); studentName.setText("Harshit Dwivedi"); } } Kotlin import kotlinx.android.synthetic.main.activity_main.* class KotlinActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { studentName.text = "Harshit" //That's it! } }
  • 8. NPE Safety var name_1: String = "abc" name_1 = null // try to assign null, won’t compile. // To allow nulls, we declare a variable as nullable string var name_2: String? = "abc" name_2 = null // no compilation error. val len = name_1.length // there will be no NPE val len = name_2.length // error: variable 'name_2' can be null
  • 9. NPE Safety 1. Checking for null in conditions val len = if (name_2 != null) name_2.length else -1 2. Safe Calls (with ?.) name_2?.length // name_2.length if name_2 is not null, and null otherwise 3. The !! Operator (for NPE Lovers) - not-null assertion operator (!!) val l = b!!.length // (!!) converts any value to a non-null type // and throws an exception if the value is null
  • 10. Coroutines (experimental) ● Threads can easily eat up almost a megabyte of memory. In this context, we can call coroutines (as per the documentation) as "lightweight" threads. ● A coroutine is sitting on an actual thread pool, which is used for background execution. ● It only uses the resources when it needs it.
  • 11. launch{} val job = launch { val userString = fetchUserString("1") val user = deserializeUser(userString) log(user.name) } The wrapped code is dispatched to a background thread, and the function itself returns a Job instance, which can be used in other coroutines to control the execution.
  • 12. async{} val user = async { val userString = fetchUserString("1") val user = deserializeUser(userString) user }.await() The async function returns a Defered<T> instance, and calling await() on it you can get the actual result of the computation.
  • 15. Type Declaration Java int a = 1; String s = “abc”; boolean b; Kotlin val a: Int = 1 val s: String = “abc” val b: Boolean
  • 16. Type Inference Java int a = 1; String s = “abc”; boolean b; Kotlin val a = 1 val s = “abc” val b: Boolean
  • 17. Object Declaration - No ‘new’ Java Car car = new Car(); Kotlin val car = Car()
  • 18. String templates Java String s = String.format(“%s has %d apples, name, count); Kotlin val s = “$name has $count apples”
  • 19. Functions Java String getName(Person person) { return person.getName(); } Kotlin fun getName(person: Person): String { return person.name }
  • 20. Expressions body Java int sum(int a , int b) { return a+b; } Kotlin val sum(a: Int , b:Int) = a+b
  • 21. ‘When’ Expression Java switch(x) { case 1: log.info("x == 1"); break; case 2: log.info("x == 2"); break; default: log.info("x is neither 1 nor 2"); } Kotlin when (x) { in 0,1 -> print("x is too low") in 2..10 -> print("x is in range") !in 10..20 -> print("x outside range") parseInt(s) -> print("s encodes x") is Program -> print("It's a program!") else -> print("None of the above") }
  • 22. Ranges Java IntStream.rangeClosed(1, 5) .forEach(x -> ...); Kotlin for (x in 1..5) { ... }
  • 23. Data Classes Java class Address { final String city; Address(String n) { city = n; } String getCity() { return city; } void setCity(String n) { city = n; } int equals() { ... } hashCode() { ... } toString() { ... } copy() { ... } } Kotlin data class Address(var city:String, var country:Country)
  • 24. Kotlin 1.2 Sharing Code between Platforms
  • 25. ● JavaScript target, allowing you to compile Kotlin code to JS and to run it in your browser ● Reuse code between the JVM and JavaScript ● Business logic of your application once, and reuse it across all tiers of your application – the backend, the browser frontend and the Android mobile app