SlideShare a Scribd company logo
Introduction of Kotlin
Language
Jieyi Wu
2017.04.20
Hello Kotlin!
fun main(args: Array<String>) {
println("Hello World!!")
}
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!!");
}
}
1. Extension Method
“
Throw away your java Utility Class !!
FileUtils AppUtils
TimeUtils
Utilities Class in Java
public final class AppUtils {
private AppUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
public static String getAppName(Context context) {
return getAppName(context, context.getPackageName());
}
public static String getAppName(Context context, String packageName) {
if (isSpace(packageName)) return null;
try {
PackageManager pm = context.getPackageManager();
PackageInfo pi = pm.getPackageInfo(packageName, 0);
return pi == null ? null : pi.applicationInfo.loadLabel(pm).toString();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return null;
}
}
……
}
public class MyActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppUtils.getAppIcon(this);
}
}
Define
Usage
Extension Method in Kotlin
inline fun Context.getAppName(): String = this.packageName
inline fun Context.getAppIcon(): Drawable? = this.getAppIcon(this.packageName)
inline fun Context.getAppIcon(packageName: String): Drawable? {
if (isSpace(packageName)) return null
try {
val pm = this.packageManager
val pi = pm.getPackageInfo(packageName, 0)
return if (null == pi) null else pi.applicationInfo.loadIcon(pm)
}
catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
return null
}
}
Define
Usage
class App: Application() {
override fun onCreate() {
super.onCreate()
// equal `this.getAppIcon()`
getAppIcon()
}
}
2. Lambdas
“✘Find the restrooms which are the distance
of 5 kilometers from me and public Toilet.
The restroom list is sorted and the restroom
name only.
data class Toilet(val id: Int,
val name: String,
val quantity: Int,
val type: Int,
val isPublic: Boolean,
val distance: Double)
Thinking
5 km Public Sorted Name
Java Code
List<Toilet> newList = new ArrayList<>();
List<String> resultList = new ArrayList<>();
list.forEach(toilet -> {
if (5 >= toilet.getDistance() && toilet.isPublic()) {
newList.add(toilet);
}
});
newList.sort((o1, o2) -> (int) (o2.getDistance() - o1.getDistance()));
newList.forEach(toilet -> resultList.add(toilet.getName()));
Kotlin Code
listToliets.filter { it.distance > 5 }.
filterNot { it.isPublic }.
sortedWith(compareBy({ it.distance })).
map { it.name }.
orEmpty()
3. DSL (Domain Specific
Language)
“✘Builder
✘Listener
✘Anko
DSL in Java
class Car {
private String name;
private double price;
private String model;
private int year;
private Car(String name, double price, String
model, int year) {
this.name = name;
this.price = price;
this.model = model;
this.year = year;
}
// getter and setter
// ...
}
static class Builder {
private String name;
private double price;
private String model;
private int year;
public Builder setName(String name) {
this.name = name;
return this;
}
public Builder setPrice(double price) {
this.price = price;
return this;
}
// ...
public Car build() {
return new Car(this.name, this.price, this.model,
this.year)
}
}
new Car.Builder()
.setName("Car")
.setPrice(12000)
.build();
Usage
Define
DSL in Kotlin
class Car(val name: String?, val price: Double, val model: String?, val year:
Int) {
private constructor(builder: Builder):
this(builder.name, builder.price, builder.model, builder.year)
companion object {
inline fun build(block: Builder.() -> Unit) =
Builder().apply(block).build()
}
internal class Builder() {
var name: String? = null
var price: Double = .0
var model: String? = null
var year: Int = 0
fun build() = Car(this)
}
}
val car = Car.build {
name = "CAR"
price = 1000.0
model = "BMW"
}
Usage
Define
Application
is developed
by Kotlin &
Java
Nice to meet Kotlin
Nice to meet Kotlin
thanks!
Any questions?
You can find me at
@Jieyi
jieyi.wu@colverlan.jp

More Related Content

What's hot (20)

PDF
Clojure for Java developers - Stockholm
Jan Kronquist
 
PPTX
Optimizing Tcl Bytecode
Donal Fellows
 
PPS
Making an Object System with Tcl 8.5
Donal Fellows
 
PDF
Kotlin Bytecode Generation and Runtime Performance
intelliyole
 
PDF
Lambdas and Streams Master Class Part 2
José Paumard
 
PPTX
Adventures in TclOO
Donal Fellows
 
PPT
JDK1.7 features
india_mani
 
PDF
Feel of Kotlin (Berlin JUG 16 Apr 2015)
intelliyole
 
PDF
JavaOne 2013 - Clojure for Java Developers
Jan Kronquist
 
PDF
Javascript development done right
Pawel Szulc
 
PDF
The TclQuadcode Compiler
Donal Fellows
 
PPT
JDBC Core Concept
Rays Technologies
 
PPTX
#5 (Remote Method Invocation)
Ghadeer AlHasan
 
PDF
Java 8 Streams & Collectors : the Leuven edition
José Paumard
 
PPTX
TclOO: Past Present Future
Donal Fellows
 
PPTX
ES6 in Real Life
Domenic Denicola
 
PDF
Kotlin: a better Java
Nils Breunese
 
PDF
Programming with Python and PostgreSQL
Peter Eisentraut
 
PDF
The Ring programming language version 1.7 book - Part 30 of 196
Mahmoud Samir Fayed
 
PPTX
Pattern Matching in Java 14
GlobalLogic Ukraine
 
Clojure for Java developers - Stockholm
Jan Kronquist
 
Optimizing Tcl Bytecode
Donal Fellows
 
Making an Object System with Tcl 8.5
Donal Fellows
 
Kotlin Bytecode Generation and Runtime Performance
intelliyole
 
Lambdas and Streams Master Class Part 2
José Paumard
 
Adventures in TclOO
Donal Fellows
 
JDK1.7 features
india_mani
 
Feel of Kotlin (Berlin JUG 16 Apr 2015)
intelliyole
 
JavaOne 2013 - Clojure for Java Developers
Jan Kronquist
 
Javascript development done right
Pawel Szulc
 
The TclQuadcode Compiler
Donal Fellows
 
JDBC Core Concept
Rays Technologies
 
#5 (Remote Method Invocation)
Ghadeer AlHasan
 
Java 8 Streams & Collectors : the Leuven edition
José Paumard
 
TclOO: Past Present Future
Donal Fellows
 
ES6 in Real Life
Domenic Denicola
 
Kotlin: a better Java
Nils Breunese
 
Programming with Python and PostgreSQL
Peter Eisentraut
 
The Ring programming language version 1.7 book - Part 30 of 196
Mahmoud Samir Fayed
 
Pattern Matching in Java 14
GlobalLogic Ukraine
 

Similar to Nice to meet Kotlin (20)

PDF
Why Kotlin is your next language?
Aliaksei Zhynhiarouski
 
PDF
Little Helpers for Android Development with Kotlin
Kai Koenig
 
PDF
Kotlin in action
Ciro Rizzo
 
PDF
9054799 dzone-refcard267-kotlin
Zoran Stanimirovic
 
PPTX
Introduction to Kotlin Language and its application to Android platform
EastBanc Tachnologies
 
PDF
Intro to Kotlin
Magda Miu
 
PDF
Let's fly to the Kotlin Island. Just an introduction to Kotlin
Aliaksei Zhynhiarouski
 
PPTX
Android & Kotlin - The code awakens #01
Omar Miatello
 
PDF
Having Fun with Kotlin Android - DILo Surabaya
DILo Surabaya
 
PDF
Kotlin a problem solver - gdd extended pune
Hardik Trivedi
 
PDF
A quick and fast intro to Kotlin
XPeppers
 
PDF
Kotlin, smarter development for the jvm
Arnaud Giuliani
 
PDF
Coding for Android on steroids with Kotlin
Kai Koenig
 
PDF
Devoxx Ukraine 2018 - Kotlin DSL in under an hour
Anton Arhipov
 
PDF
Summer of Tech 2017 - Kotlin/Android bootcamp
Kai Koenig
 
PDF
Practical tips for building apps with kotlin
Adit Lal
 
PDF
Develop your next app with kotlin @ AndroidMakersFr 2017
Arnaud Giuliani
 
PPTX
Android & Kotlin - The code awakens #03
Omar Miatello
 
PDF
Introduction to kotlin
NAVER Engineering
 
PPTX
Using Kotlin, to Create Kotlin, to Teach Kotlin, in Space
Garth Gilmour
 
Why Kotlin is your next language?
Aliaksei Zhynhiarouski
 
Little Helpers for Android Development with Kotlin
Kai Koenig
 
Kotlin in action
Ciro Rizzo
 
9054799 dzone-refcard267-kotlin
Zoran Stanimirovic
 
Introduction to Kotlin Language and its application to Android platform
EastBanc Tachnologies
 
Intro to Kotlin
Magda Miu
 
Let's fly to the Kotlin Island. Just an introduction to Kotlin
Aliaksei Zhynhiarouski
 
Android & Kotlin - The code awakens #01
Omar Miatello
 
Having Fun with Kotlin Android - DILo Surabaya
DILo Surabaya
 
Kotlin a problem solver - gdd extended pune
Hardik Trivedi
 
A quick and fast intro to Kotlin
XPeppers
 
Kotlin, smarter development for the jvm
Arnaud Giuliani
 
Coding for Android on steroids with Kotlin
Kai Koenig
 
Devoxx Ukraine 2018 - Kotlin DSL in under an hour
Anton Arhipov
 
Summer of Tech 2017 - Kotlin/Android bootcamp
Kai Koenig
 
Practical tips for building apps with kotlin
Adit Lal
 
Develop your next app with kotlin @ AndroidMakersFr 2017
Arnaud Giuliani
 
Android & Kotlin - The code awakens #03
Omar Miatello
 
Introduction to kotlin
NAVER Engineering
 
Using Kotlin, to Create Kotlin, to Teach Kotlin, in Space
Garth Gilmour
 
Ad

More from Jieyi Wu (10)

PPTX
Design pattern advanced ii with testing
Jieyi Wu
 
PDF
Design pattern advanced i
Jieyi Wu
 
PDF
Dependency injection
Jieyi Wu
 
PDF
Application architecture pattern
Jieyi Wu
 
PDF
Reactive X introduction part1
Jieyi Wu
 
PDF
Karitoke's supported technology
Jieyi Wu
 
PPTX
Design pattern - part 3
Jieyi Wu
 
PPTX
Design pattern part 2 - structural pattern
Jieyi Wu
 
PPTX
Design pattern - part 1
Jieyi Wu
 
PPTX
Object-oriented programming
Jieyi Wu
 
Design pattern advanced ii with testing
Jieyi Wu
 
Design pattern advanced i
Jieyi Wu
 
Dependency injection
Jieyi Wu
 
Application architecture pattern
Jieyi Wu
 
Reactive X introduction part1
Jieyi Wu
 
Karitoke's supported technology
Jieyi Wu
 
Design pattern - part 3
Jieyi Wu
 
Design pattern part 2 - structural pattern
Jieyi Wu
 
Design pattern - part 1
Jieyi Wu
 
Object-oriented programming
Jieyi Wu
 
Ad

Recently uploaded (20)

PPTX
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
PDF
Natural Language processing and web deigning notes
AnithaSakthivel3
 
PDF
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
PPT
IISM Presentation.ppt Construction safety
lovingrkn
 
PPTX
Unit II: Meteorology of Air Pollution and Control Engineering:
sundharamm
 
PDF
Geothermal Heat Pump ppt-SHRESTH S KOKNE
SHRESTHKOKNE
 
PPTX
UNIT III CONTROL OF PARTICULATE CONTAMINANTS
sundharamm
 
PPTX
Basics of Auto Computer Aided Drafting .pptx
Krunal Thanki
 
PPTX
Water resources Engineering GIS KRT.pptx
Krunal Thanki
 
PDF
Introduction to Robotics Mechanics and Control 4th Edition by John J. Craig S...
solutionsmanual3
 
PPTX
ENSA_Module_7.pptx_wide_area_network_concepts
RanaMukherjee24
 
PDF
勉強会資料_An Image is Worth More Than 16x16 Patches
NABLAS株式会社
 
PDF
NOISE CONTROL ppt - SHRESTH SUDHIR KOKNE
SHRESTHKOKNE
 
PDF
CFM 56-7B - Engine General Familiarization. PDF
Gianluca Foro
 
PDF
Farm Machinery and Equipments Unit 1&2.pdf
prabhum311
 
PPTX
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
PDF
Air -Powered Car PPT by ER. SHRESTH SUDHIR KOKNE.pdf
SHRESTHKOKNE
 
PDF
SG1-ALM-MS-EL-30-0008 (00) MS - Isolators and disconnecting switches.pdf
djiceramil
 
PPTX
Introduction to Fluid and Thermal Engineering
Avesahemad Husainy
 
PDF
Non Text Magic Studio Magic Design for Presentations L&P.pdf
rajpal7872
 
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
Natural Language processing and web deigning notes
AnithaSakthivel3
 
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
IISM Presentation.ppt Construction safety
lovingrkn
 
Unit II: Meteorology of Air Pollution and Control Engineering:
sundharamm
 
Geothermal Heat Pump ppt-SHRESTH S KOKNE
SHRESTHKOKNE
 
UNIT III CONTROL OF PARTICULATE CONTAMINANTS
sundharamm
 
Basics of Auto Computer Aided Drafting .pptx
Krunal Thanki
 
Water resources Engineering GIS KRT.pptx
Krunal Thanki
 
Introduction to Robotics Mechanics and Control 4th Edition by John J. Craig S...
solutionsmanual3
 
ENSA_Module_7.pptx_wide_area_network_concepts
RanaMukherjee24
 
勉強会資料_An Image is Worth More Than 16x16 Patches
NABLAS株式会社
 
NOISE CONTROL ppt - SHRESTH SUDHIR KOKNE
SHRESTHKOKNE
 
CFM 56-7B - Engine General Familiarization. PDF
Gianluca Foro
 
Farm Machinery and Equipments Unit 1&2.pdf
prabhum311
 
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
Air -Powered Car PPT by ER. SHRESTH SUDHIR KOKNE.pdf
SHRESTHKOKNE
 
SG1-ALM-MS-EL-30-0008 (00) MS - Isolators and disconnecting switches.pdf
djiceramil
 
Introduction to Fluid and Thermal Engineering
Avesahemad Husainy
 
Non Text Magic Studio Magic Design for Presentations L&P.pdf
rajpal7872
 

Nice to meet Kotlin

  • 2. Hello Kotlin! fun main(args: Array<String>) { println("Hello World!!") } public class Main { public static void main(String[] args) { System.out.println("Hello World!!"); } }
  • 4. “ Throw away your java Utility Class !! FileUtils AppUtils TimeUtils
  • 5. Utilities Class in Java public final class AppUtils { private AppUtils() { throw new UnsupportedOperationException("u can't instantiate me..."); } public static String getAppName(Context context) { return getAppName(context, context.getPackageName()); } public static String getAppName(Context context, String packageName) { if (isSpace(packageName)) return null; try { PackageManager pm = context.getPackageManager(); PackageInfo pi = pm.getPackageInfo(packageName, 0); return pi == null ? null : pi.applicationInfo.loadLabel(pm).toString(); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return null; } } …… } public class MyActivity extends Activity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AppUtils.getAppIcon(this); } } Define Usage
  • 6. Extension Method in Kotlin inline fun Context.getAppName(): String = this.packageName inline fun Context.getAppIcon(): Drawable? = this.getAppIcon(this.packageName) inline fun Context.getAppIcon(packageName: String): Drawable? { if (isSpace(packageName)) return null try { val pm = this.packageManager val pi = pm.getPackageInfo(packageName, 0) return if (null == pi) null else pi.applicationInfo.loadIcon(pm) } catch (e: PackageManager.NameNotFoundException) { e.printStackTrace() return null } } Define Usage class App: Application() { override fun onCreate() { super.onCreate() // equal `this.getAppIcon()` getAppIcon() } }
  • 8. “✘Find the restrooms which are the distance of 5 kilometers from me and public Toilet. The restroom list is sorted and the restroom name only. data class Toilet(val id: Int, val name: String, val quantity: Int, val type: Int, val isPublic: Boolean, val distance: Double)
  • 9. Thinking 5 km Public Sorted Name
  • 10. Java Code List<Toilet> newList = new ArrayList<>(); List<String> resultList = new ArrayList<>(); list.forEach(toilet -> { if (5 >= toilet.getDistance() && toilet.isPublic()) { newList.add(toilet); } }); newList.sort((o1, o2) -> (int) (o2.getDistance() - o1.getDistance())); newList.forEach(toilet -> resultList.add(toilet.getName()));
  • 11. Kotlin Code listToliets.filter { it.distance > 5 }. filterNot { it.isPublic }. sortedWith(compareBy({ it.distance })). map { it.name }. orEmpty()
  • 12. 3. DSL (Domain Specific Language)
  • 14. DSL in Java class Car { private String name; private double price; private String model; private int year; private Car(String name, double price, String model, int year) { this.name = name; this.price = price; this.model = model; this.year = year; } // getter and setter // ... } static class Builder { private String name; private double price; private String model; private int year; public Builder setName(String name) { this.name = name; return this; } public Builder setPrice(double price) { this.price = price; return this; } // ... public Car build() { return new Car(this.name, this.price, this.model, this.year) } } new Car.Builder() .setName("Car") .setPrice(12000) .build(); Usage Define
  • 15. DSL in Kotlin class Car(val name: String?, val price: Double, val model: String?, val year: Int) { private constructor(builder: Builder): this(builder.name, builder.price, builder.model, builder.year) companion object { inline fun build(block: Builder.() -> Unit) = Builder().apply(block).build() } internal class Builder() { var name: String? = null var price: Double = .0 var model: String? = null var year: Int = 0 fun build() = Car(this) } } val car = Car.build { name = "CAR" price = 1000.0 model = "BMW" } Usage Define