SlideShare ist ein Scribd-Unternehmen logo
1 von 29
Downloaden Sie, um offline zu lesen
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:

Weitere ähnliche Inhalte

Was ist angesagt?

JJUG CCC 2011 Spring
JJUG CCC 2011 SpringJJUG CCC 2011 Spring
JJUG CCC 2011 Spring
Kiyotaka Oku
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
Dmitry Buzdin
 
Poor Man's Functional Programming
Poor Man's Functional ProgrammingPoor Man's Functional Programming
Poor Man's Functional Programming
Dmitry Buzdin
 

Was ist angesagt? (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
 

Ähnlich wie Introduction kot iin

pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
AST Transformations at JFokus
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokus
HamletDRC
 
AST Transformations
AST TransformationsAST Transformations
AST Transformations
HamletDRC
 
Intro to scala
Intro to scalaIntro to scala
Intro to scala
Joe Zulli
 

Ähnlich wie 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
 

Kürzlich hochgeladen

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Kürzlich hochgeladen (20)

Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 

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 )