SlideShare une entreprise Scribd logo
1  sur  29
Télécharger pour lire hors ligne
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:

Contenu connexe

Tendances

多治見IT勉強会 Groovy Grails
多治見IT勉強会 Groovy Grails多治見IT勉強会 Groovy Grails
多治見IT勉強会 Groovy GrailsTsuyoshi Yamamoto
 
EcmaScript 6 - The future is here
EcmaScript 6 - The future is hereEcmaScript 6 - The future is here
EcmaScript 6 - The future is hereSebastiano Armeli
 
JJUG CCC 2011 Spring
JJUG CCC 2011 SpringJJUG CCC 2011 Spring
JJUG CCC 2011 SpringKiyotaka Oku
 
Ian 20150116 java script oop
Ian 20150116 java script oopIan 20150116 java script oop
Ian 20150116 java script oopLearningTech
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with ClojureDmitry Buzdin
 
Javascript ES6 generators
Javascript ES6 generatorsJavascript ES6 generators
Javascript ES6 generatorsRamesh Nair
 
Jakarta Commons - Don't re-invent the wheel
Jakarta Commons - Don't re-invent the wheelJakarta Commons - Don't re-invent the wheel
Jakarta Commons - Don't re-invent the wheeltcurdt
 
No dark magic - Byte code engineering in the real world
No dark magic - Byte code engineering in the real worldNo dark magic - Byte code engineering in the real world
No dark magic - Byte code engineering in the real worldtcurdt
 
Scalaz 8 vs Akka Actors
Scalaz 8 vs Akka ActorsScalaz 8 vs Akka Actors
Scalaz 8 vs Akka ActorsJohn De Goes
 
Programming with Python and PostgreSQL
Programming with Python and PostgreSQLProgramming with Python and PostgreSQL
Programming with Python and PostgreSQLPeter Eisentraut
 
Poor Man's Functional Programming
Poor Man's Functional ProgrammingPoor Man's Functional Programming
Poor Man's Functional ProgrammingDmitry Buzdin
 
Auto-GWT : Better GWT Programming with Xtend
Auto-GWT : Better GWT Programming with XtendAuto-GWT : Better GWT Programming with Xtend
Auto-GWT : Better GWT Programming with XtendSven Efftinge
 
GPars For Beginners
GPars For BeginnersGPars For Beginners
GPars For BeginnersMatt Passell
 
Dts x dicoding #2 memulai pemrograman kotlin
Dts x dicoding #2 memulai pemrograman kotlinDts x dicoding #2 memulai pemrograman kotlin
Dts x dicoding #2 memulai pemrograman kotlinAhmad Arif Faizin
 
Hello kotlin | An Event by DSC Unideb
Hello kotlin | An Event by DSC UnidebHello kotlin | An Event by DSC Unideb
Hello kotlin | An Event by DSC UnidebMuhammad Raza
 

Tendances (20)

多治見IT勉強会 Groovy Grails
多治見IT勉強会 Groovy Grails多治見IT勉強会 Groovy Grails
多治見IT勉強会 Groovy Grails
 
ES6 in Real Life
ES6 in Real LifeES6 in Real Life
ES6 in Real Life
 
EcmaScript 6 - The future is here
EcmaScript 6 - The future is hereEcmaScript 6 - The future is here
EcmaScript 6 - The future is here
 
JJUG CCC 2011 Spring
JJUG CCC 2011 SpringJJUG CCC 2011 Spring
JJUG CCC 2011 Spring
 
Ian 20150116 java script oop
Ian 20150116 java script oopIan 20150116 java script oop
Ian 20150116 java script oop
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
Sneaking inside Kotlin features
Sneaking inside Kotlin featuresSneaking inside Kotlin features
Sneaking inside Kotlin features
 
Javascript ES6 generators
Javascript ES6 generatorsJavascript ES6 generators
Javascript ES6 generators
 
ECMAScript 6
ECMAScript 6ECMAScript 6
ECMAScript 6
 
Jakarta Commons - Don't re-invent the wheel
Jakarta Commons - Don't re-invent the wheelJakarta Commons - Don't re-invent the wheel
Jakarta Commons - Don't re-invent the wheel
 
EcmaScript 6
EcmaScript 6 EcmaScript 6
EcmaScript 6
 
No dark magic - Byte code engineering in the real world
No dark magic - Byte code engineering in the real worldNo dark magic - Byte code engineering in the real world
No dark magic - Byte code engineering in the real world
 
Scalaz 8 vs Akka Actors
Scalaz 8 vs Akka ActorsScalaz 8 vs Akka Actors
Scalaz 8 vs Akka Actors
 
Programming with Python and PostgreSQL
Programming with Python and PostgreSQLProgramming with Python and PostgreSQL
Programming with Python and PostgreSQL
 
Poor Man's Functional Programming
Poor Man's Functional ProgrammingPoor Man's Functional Programming
Poor Man's Functional Programming
 
Auto-GWT : Better GWT Programming with Xtend
Auto-GWT : Better GWT Programming with XtendAuto-GWT : Better GWT Programming with Xtend
Auto-GWT : Better GWT Programming with Xtend
 
GPars For Beginners
GPars For BeginnersGPars For Beginners
GPars For Beginners
 
Dts x dicoding #2 memulai pemrograman kotlin
Dts x dicoding #2 memulai pemrograman kotlinDts x dicoding #2 memulai pemrograman kotlin
Dts x dicoding #2 memulai pemrograman kotlin
 
Gwt and Xtend
Gwt and XtendGwt and Xtend
Gwt and Xtend
 
Hello kotlin | An Event by DSC Unideb
Hello kotlin | An Event by DSC UnidebHello kotlin | An Event by DSC Unideb
Hello kotlin | An Event by DSC Unideb
 

Similaire à Introduction kot iin

Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018
Meetup di GDG Italia - Leonardo Pirro -  Codemotion Rome 2018 Meetup di GDG Italia - Leonardo Pirro -  Codemotion Rome 2018
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018 Codemotion
 
Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Cody Engel
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
つくってあそぼ Kotlin DSL ~拡張編~
つくってあそぼ Kotlin DSL ~拡張編~つくってあそぼ Kotlin DSL ~拡張編~
つくってあそぼ Kotlin DSL ~拡張編~kamedon39
 
No excuses, switch to kotlin
No excuses, switch to kotlinNo excuses, switch to kotlin
No excuses, switch to kotlinThijs Suijten
 
Kotlin for Android Developers - 3
Kotlin for Android Developers - 3Kotlin for Android Developers - 3
Kotlin for Android Developers - 3Mohamed Nabil, MSc.
 
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018Codemotion
 
AST Transformations at JFokus
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokusHamletDRC
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript IntroductionDmitry Sheiko
 
AST Transformations
AST TransformationsAST Transformations
AST TransformationsHamletDRC
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldBTI360
 
Intro to scala
Intro to scalaIntro to scala
Intro to scalaJoe Zulli
 
JavaScript Survival Guide
JavaScript Survival GuideJavaScript Survival Guide
JavaScript Survival GuideGiordano Scalzo
 
Kotlin, a modern language for modern times
Kotlin, a modern language for modern timesKotlin, a modern language for modern times
Kotlin, a modern language for modern timesSergi Martínez
 

Similaire à Introduction kot iin (20)

Kotlin
KotlinKotlin
Kotlin
 
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018
Meetup di GDG Italia - Leonardo Pirro -  Codemotion Rome 2018 Meetup di GDG Italia - Leonardo Pirro -  Codemotion Rome 2018
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018
 
Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
つくってあそぼ Kotlin DSL ~拡張編~
つくってあそぼ Kotlin DSL ~拡張編~つくってあそぼ Kotlin DSL ~拡張編~
つくってあそぼ Kotlin DSL ~拡張編~
 
No excuses, switch to kotlin
No excuses, switch to kotlinNo excuses, switch to kotlin
No excuses, switch to kotlin
 
Kotlin for Android Developers - 3
Kotlin for Android Developers - 3Kotlin for Android Developers - 3
Kotlin for Android Developers - 3
 
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
 
AST Transformations at JFokus
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokus
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
AST Transformations
AST TransformationsAST Transformations
AST Transformations
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 World
 
Intro to scala
Intro to scalaIntro to scala
Intro to scala
 
JavaScript Survival Guide
JavaScript Survival GuideJavaScript Survival Guide
JavaScript Survival Guide
 
Swift Study #7
Swift Study #7Swift Study #7
Swift Study #7
 
Kotlin, a modern language for modern times
Kotlin, a modern language for modern timesKotlin, a modern language for modern times
Kotlin, a modern language for modern times
 

Dernier

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 

Dernier (20)

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 

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 )