SlideShare a Scribd company logo
Introduction to KotIin
What’s Kotlin
● New language support by Jetbrain
● Run on JVM
● Static type
● 100% interoperable with Java
● https://try.kotlinlang.org/
● Declaration type (val/ var)
● Property
● Function
● Lambda
● Higher order function
● Data class
Agenda
2 Types of Variable
(Mutable & Immutable)
Java Kotlin
Person person = new Person();
int number = 10;
var (variable: mutable)
String name = “Kotlin”;
String surname = “Pro”
surname = “melon”
var name: String = “Kotlin”
var surname = “Pro”
surname = “melon”
var number = 10
var person = Person()
val (value: immutable)
Java Kotlin
final String name = “Kotlin”;
final int number = 10;
final Person person = new Person();
val name: String = “Kotlin”
val surname = “Pro”
surname = “melon”
val number = 10
val person = Person()
Properties
(Field + Accessor)
Class & Properties
Java Kotlin
Java Kotlin Output
person.setName(“Kotlin”)
System.out.print(person.getName())
person.name = “Kotlin”
print(person.name)
Kotlin
public class Person {
private String name = “melon” ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Person {
var name = “melon”
}
Properties Initialization
● Default Getter/Setter
● Custom Getter/Setter
● Late init
● Lazy
Custom Getter/Setter
Java Kotlin
Java Kotlin Output
person.setName(“Kotlin”)
System.out.print(person.getName())
person.name = “Kotlin”
print(person.name)
MY NAME IS KOTLIN
public class Person {
private String name = “”;
public String getName() {
return name.toUpperCase();
}
public void setName(String name) {
this.name = “My name is ” + name;
}
}
class Person {
var name: String = “”
get() = field.toUpperCase()
set(value) {
field = “My Name is $value”
}
}
Kotlin
class Test {
var subject: TestSubject
}
Compile error: Property must initialized
LATE INIT(1)
Kotlin
class Test {
lateinit var subject: TestSubject
@Setup fun setUp( ) {
subject = TestSubject()
}
@Test fun test ( ) {
subject.method()
}
}
Initialize later
Kotlin
class Person {
lateinit var name: String
fun getPersonInfo(): String {
return name
}
}
Input Output
print(person.name)
kotlin.UninitializedPropertyAccessException
LAZY : (Save memory and skip the initialisation until the property is required. )
Kotlin
class EditProfilePersenter {
val database by lazy {
Database()
}
fun saveToDatabase(name: Person) {
database.save(name)
…...
}
}
DATABASE OBJECT IS INITIATE AT THIS LINE.
Nullable type
TYPE? TYPE= NULLOR
var y: String = “Hi”
y = null
var x: String? = “Hi”
x = null
Compile error: y can’t be null
Java Kotlin
Assertion !!
Elvis operator ?:
Safety operator ?
String getUppercaseName (String name) {
if (name != null) {
return name.toUpperCase();
}
return null;
}
String getUppercaseName (String name) {
if (name != null) {
return name.toUpperCase();
}
return “”;
}
String getUppercaseName (String name) {
return name.toUpperCase();
}
fun getUppercaseName (name: String?) : String? {
return name?.toUpperCase()
}
fun getUppercaseName (name: String?) : String {
return name?.toUpperCase() ?: “”
}
fun getUppercaseName (name: String?) : String {
return name!!.toUpperCase()
}
Constructor
(constructor block + initialize block)
Java Kotlin
Constructor block
Initialize block
How to use : Person(“Kotlin”)
Constructor block
Initialize block
public class Person {
Person(String name) {
System.out.println(name);
}
}
class Person(name: String) {
init {
println(name)
}
}
public class Person {
private String name;
Person(String name) {
this.name = name;
System.out.println(name);
}
public String getNameUpperCase() {
return name.toUpperCase()
}
}
class Person(val name: String) {
init {
println(name)
}
fun getNameUpperCase() : String {
return name.toUpperCase()
}
}
Function
Java Kotlin
Usage:
plus(1,2) outcome: 3
plus(x=1, y=2) outcome: 3
plus(y=2, x=1) outcome: 3
plus(y=2) outcome: 2 <— x+y = 0+2 = 2
public int plus(int x, int y) {
return x+y;
}
fun plus(x: Int, y: Int) : Int {
return x+y
}
fun plus(x: Int, y: Int) = x+y
Single Expression
fun plus(x: Int = 0, y: Int) = x+y
Parameter can set default value
Lambda
(Anonymous function)
(Parameter Type) Return Type
val resultOfsum = { x: Int, y:Int -> x + y }
val resultOfsum = sum(2, 3)
fun sum(x: Int, y: Int) {
return x + y
}
Usage:
resultOfsum(2, 2) outcome: 4
resultOfsum(2, 3) outcome: 5
(Int, Int) Int
Implement something{ }
Higher order function
(A function that takes another function as an argument or returns one.)
fun twoAndThreeOperation(operation: (Int, Int) -> Int) {
val result = operation(2, 3)
println("The result is $result")
}
Higher order function
Usage:
twoAndThreeOperation ({ x, y —> x + y}) outcome : The result is 5 //x=2, y= 3 → 2+3
twoAndThreeOperation { x, y —> x * y} outcome : The result is 6 //x=2, y= 3 → 2*3
twoAndThreeOperation { x, y —> y - x + 10 } outcome : The result is 11 //x=2, y= 3 → 3-2+10
public inline fun <T, R> Iterable<T>.map(transform: (T) -> R): List<R> {
return mapTo(ArrayList<R>(collectionSizeOrDefault(10)), transform)
}
person1: Person(name = “A”, age = 20)
person2: Person(name = “B”, age = 21)
person3: Person(name = “C”, age = 22)
val persons = listOf(person1, person2, person3)
List Of Person List Of person’s name
persons.map { person -> person.name }
map (transform)
List Of Person List Of person’s age
persons.map { person -> person.age }
map (transform)
Java :
List<String> personName = new ArrayList<> ();
for (Person person: persons) {
personName.add(person.name)
}
Java :
List<Integer> personAge = new ArrayList<>();
for (Person person: persons) {
personAge.add(person.age)
}
Data class
Java Kotlin
class Person {
private String name;
private int age;
@Override
String toString() {}
@Override
int hashCode() {}
@Override
boolean equals(Object o) {}
Person copy() {}
….getter()/setter() of name, age….
}
data class Person (
val name: String,
var age: Int
)
Sponsors by:

More Related Content

What's hot (20)

PDF
多治見IT勉強会 Groovy Grails
Tsuyoshi Yamamoto
 
PPTX
ES6 in Real Life
Domenic Denicola
 
PDF
EcmaScript 6 - The future is here
Sebastiano Armeli
 
PDF
JJUG CCC 2011 Spring
Kiyotaka Oku
 
PPTX
Ian 20150116 java script oop
LearningTech
 
PDF
Refactoring to Macros with Clojure
Dmitry Buzdin
 
PDF
Sneaking inside Kotlin features
Chandra Sekhar Nayak
 
PDF
Javascript ES6 generators
RameshNair6
 
PDF
ECMAScript 6
Piotr Lewandowski
 
PDF
Jakarta Commons - Don't re-invent the wheel
tcurdt
 
ODP
EcmaScript 6
Manoj Kumar
 
PDF
No dark magic - Byte code engineering in the real world
tcurdt
 
PDF
Scalaz 8 vs Akka Actors
John De Goes
 
PDF
Programming with Python and PostgreSQL
Peter Eisentraut
 
PPTX
Poor Man's Functional Programming
Dmitry Buzdin
 
PDF
Auto-GWT : Better GWT Programming with Xtend
Sven Efftinge
 
PDF
GPars For Beginners
Matt Passell
 
PPTX
Dts x dicoding #2 memulai pemrograman kotlin
Ahmad Arif Faizin
 
KEY
Gwt and Xtend
Sven Efftinge
 
PPTX
Hello kotlin | An Event by DSC Unideb
Muhammad Raza
 
多治見IT勉強会 Groovy Grails
Tsuyoshi Yamamoto
 
ES6 in Real Life
Domenic Denicola
 
EcmaScript 6 - The future is here
Sebastiano Armeli
 
JJUG CCC 2011 Spring
Kiyotaka Oku
 
Ian 20150116 java script oop
LearningTech
 
Refactoring to Macros with Clojure
Dmitry Buzdin
 
Sneaking inside Kotlin features
Chandra Sekhar Nayak
 
Javascript ES6 generators
RameshNair6
 
ECMAScript 6
Piotr Lewandowski
 
Jakarta Commons - Don't re-invent the wheel
tcurdt
 
EcmaScript 6
Manoj Kumar
 
No dark magic - Byte code engineering in the real world
tcurdt
 
Scalaz 8 vs Akka Actors
John De Goes
 
Programming with Python and PostgreSQL
Peter Eisentraut
 
Poor Man's Functional Programming
Dmitry Buzdin
 
Auto-GWT : Better GWT Programming with Xtend
Sven Efftinge
 
GPars For Beginners
Matt Passell
 
Dts x dicoding #2 memulai pemrograman kotlin
Ahmad Arif Faizin
 
Gwt and Xtend
Sven Efftinge
 
Hello kotlin | An Event by DSC Unideb
Muhammad Raza
 

Similar to Introduction kot iin (20)

PDF
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018
Codemotion
 
PDF
Privet Kotlin (Windy City DevFest)
Cody Engel
 
PDF
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
PDF
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
PDF
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
PDF
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
PDF
つくってあそぼ Kotlin DSL ~拡張編~
kamedon39
 
PDF
No excuses, switch to kotlin
Thijs Suijten
 
PDF
Kotlin for Android Developers - 3
Mohamed Nabil, MSc.
 
PDF
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Codemotion
 
ODP
AST Transformations at JFokus
HamletDRC
 
PDF
TypeScript Introduction
Dmitry Sheiko
 
ODP
AST Transformations
HamletDRC
 
PDF
Introduction to Scala
Aleksandar Prokopec
 
PDF
Scala vs Java 8 in a Java 8 World
BTI360
 
PPTX
Intro to scala
Joe Zulli
 
PDF
JavaScript Survival Guide
Giordano Scalzo
 
PDF
Swift Study #7
chanju Jeon
 
PDF
Kotlin, a modern language for modern times
Sergi Martínez
 
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018
Codemotion
 
Privet Kotlin (Windy City DevFest)
Cody Engel
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
つくってあそぼ Kotlin DSL ~拡張編~
kamedon39
 
No excuses, switch to kotlin
Thijs Suijten
 
Kotlin for Android Developers - 3
Mohamed Nabil, MSc.
 
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Codemotion
 
AST Transformations at JFokus
HamletDRC
 
TypeScript Introduction
Dmitry Sheiko
 
AST Transformations
HamletDRC
 
Introduction to Scala
Aleksandar Prokopec
 
Scala vs Java 8 in a Java 8 World
BTI360
 
Intro to scala
Joe Zulli
 
JavaScript Survival Guide
Giordano Scalzo
 
Swift Study #7
chanju Jeon
 
Kotlin, a modern language for modern times
Sergi Martínez
 
Ad

Recently uploaded (20)

PPTX
Digital Professionalism and Interpersonal Competence
rutvikgediya1
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PPTX
Translation_ Definition, Scope & Historical Development.pptx
DhatriParmar
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PDF
John Keats introduction and list of his important works
vatsalacpr
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PPTX
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
PPTX
LDP-2 UNIT 4 Presentation for practical.pptx
abhaypanchal2525
 
PPTX
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
PPTX
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
I INCLUDED THIS TOPIC IS INTELLIGENCE DEFINITION, MEANING, INDIVIDUAL DIFFERE...
parmarjuli1412
 
PDF
My Thoughts On Q&A- A Novel By Vikas Swarup
Niharika
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
Digital Professionalism and Interpersonal Competence
rutvikgediya1
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
Translation_ Definition, Scope & Historical Development.pptx
DhatriParmar
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
John Keats introduction and list of his important works
vatsalacpr
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
LDP-2 UNIT 4 Presentation for practical.pptx
abhaypanchal2525
 
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
Virus sequence retrieval from NCBI database
yamunaK13
 
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
I INCLUDED THIS TOPIC IS INTELLIGENCE DEFINITION, MEANING, INDIVIDUAL DIFFERE...
parmarjuli1412
 
My Thoughts On Q&A- A Novel By Vikas Swarup
Niharika
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
Ad

Introduction kot iin

  • 2. What’s Kotlin ● New language support by Jetbrain ● Run on JVM ● Static type ● 100% interoperable with Java ● https://try.kotlinlang.org/
  • 3. ● Declaration type (val/ var) ● Property ● Function ● Lambda ● Higher order function ● Data class Agenda
  • 4. 2 Types of Variable (Mutable & Immutable)
  • 5. Java Kotlin Person person = new Person(); int number = 10; var (variable: mutable) String name = “Kotlin”; String surname = “Pro” surname = “melon” var name: String = “Kotlin” var surname = “Pro” surname = “melon” var number = 10 var person = Person()
  • 6. val (value: immutable) Java Kotlin final String name = “Kotlin”; final int number = 10; final Person person = new Person(); val name: String = “Kotlin” val surname = “Pro” surname = “melon” val number = 10 val person = Person()
  • 8. Class & Properties Java Kotlin Java Kotlin Output person.setName(“Kotlin”) System.out.print(person.getName()) person.name = “Kotlin” print(person.name) Kotlin public class Person { private String name = “melon” ; public String getName() { return name; } public void setName(String name) { this.name = name; } } class Person { var name = “melon” }
  • 9. Properties Initialization ● Default Getter/Setter ● Custom Getter/Setter ● Late init ● Lazy
  • 10. Custom Getter/Setter Java Kotlin Java Kotlin Output person.setName(“Kotlin”) System.out.print(person.getName()) person.name = “Kotlin” print(person.name) MY NAME IS KOTLIN public class Person { private String name = “”; public String getName() { return name.toUpperCase(); } public void setName(String name) { this.name = “My name is ” + name; } } class Person { var name: String = “” get() = field.toUpperCase() set(value) { field = “My Name is $value” } }
  • 11. Kotlin class Test { var subject: TestSubject } Compile error: Property must initialized
  • 12. LATE INIT(1) Kotlin class Test { lateinit var subject: TestSubject @Setup fun setUp( ) { subject = TestSubject() } @Test fun test ( ) { subject.method() } } Initialize later
  • 13. Kotlin class Person { lateinit var name: String fun getPersonInfo(): String { return name } } Input Output print(person.name) kotlin.UninitializedPropertyAccessException
  • 14. LAZY : (Save memory and skip the initialisation until the property is required. ) Kotlin class EditProfilePersenter { val database by lazy { Database() } fun saveToDatabase(name: Person) { database.save(name) …... } } DATABASE OBJECT IS INITIATE AT THIS LINE.
  • 16. TYPE? TYPE= NULLOR var y: String = “Hi” y = null var x: String? = “Hi” x = null Compile error: y can’t be null
  • 17. Java Kotlin Assertion !! Elvis operator ?: Safety operator ? String getUppercaseName (String name) { if (name != null) { return name.toUpperCase(); } return null; } String getUppercaseName (String name) { if (name != null) { return name.toUpperCase(); } return “”; } String getUppercaseName (String name) { return name.toUpperCase(); } fun getUppercaseName (name: String?) : String? { return name?.toUpperCase() } fun getUppercaseName (name: String?) : String { return name?.toUpperCase() ?: “” } fun getUppercaseName (name: String?) : String { return name!!.toUpperCase() }
  • 18. Constructor (constructor block + initialize block)
  • 19. Java Kotlin Constructor block Initialize block How to use : Person(“Kotlin”) Constructor block Initialize block public class Person { Person(String name) { System.out.println(name); } } class Person(name: String) { init { println(name) } } public class Person { private String name; Person(String name) { this.name = name; System.out.println(name); } public String getNameUpperCase() { return name.toUpperCase() } } class Person(val name: String) { init { println(name) } fun getNameUpperCase() : String { return name.toUpperCase() } }
  • 21. Java Kotlin Usage: plus(1,2) outcome: 3 plus(x=1, y=2) outcome: 3 plus(y=2, x=1) outcome: 3 plus(y=2) outcome: 2 <— x+y = 0+2 = 2 public int plus(int x, int y) { return x+y; } fun plus(x: Int, y: Int) : Int { return x+y } fun plus(x: Int, y: Int) = x+y Single Expression fun plus(x: Int = 0, y: Int) = x+y Parameter can set default value
  • 23. (Parameter Type) Return Type val resultOfsum = { x: Int, y:Int -> x + y } val resultOfsum = sum(2, 3) fun sum(x: Int, y: Int) { return x + y } Usage: resultOfsum(2, 2) outcome: 4 resultOfsum(2, 3) outcome: 5 (Int, Int) Int Implement something{ }
  • 24. Higher order function (A function that takes another function as an argument or returns one.)
  • 25. fun twoAndThreeOperation(operation: (Int, Int) -> Int) { val result = operation(2, 3) println("The result is $result") } Higher order function Usage: twoAndThreeOperation ({ x, y —> x + y}) outcome : The result is 5 //x=2, y= 3 → 2+3 twoAndThreeOperation { x, y —> x * y} outcome : The result is 6 //x=2, y= 3 → 2*3 twoAndThreeOperation { x, y —> y - x + 10 } outcome : The result is 11 //x=2, y= 3 → 3-2+10
  • 26. public inline fun <T, R> Iterable<T>.map(transform: (T) -> R): List<R> { return mapTo(ArrayList<R>(collectionSizeOrDefault(10)), transform) } person1: Person(name = “A”, age = 20) person2: Person(name = “B”, age = 21) person3: Person(name = “C”, age = 22) val persons = listOf(person1, person2, person3) List Of Person List Of person’s name persons.map { person -> person.name } map (transform) List Of Person List Of person’s age persons.map { person -> person.age } map (transform) Java : List<String> personName = new ArrayList<> (); for (Person person: persons) { personName.add(person.name) } Java : List<Integer> personAge = new ArrayList<>(); for (Person person: persons) { personAge.add(person.age) }
  • 28. Java Kotlin class Person { private String name; private int age; @Override String toString() {} @Override int hashCode() {} @Override boolean equals(Object o) {} Person copy() {} ….getter()/setter() of name, age…. } data class Person ( val name: String, var age: Int )