SlideShare une entreprise Scribd logo
1  sur  19
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
thanks!
Any questions?
You can find me at
@Jieyi
jieyi.wu@colverlan.jp

Contenu connexe

Tendances

Clojure for Java developers - Stockholm
Clojure for Java developers - StockholmClojure for Java developers - Stockholm
Clojure for Java developers - Stockholm
Jan Kronquist
 
Optimizing Tcl Bytecode
Optimizing Tcl BytecodeOptimizing Tcl Bytecode
Optimizing Tcl Bytecode
Donal Fellows
 
Feel of Kotlin (Berlin JUG 16 Apr 2015)
Feel of Kotlin (Berlin JUG 16 Apr 2015)Feel of Kotlin (Berlin JUG 16 Apr 2015)
Feel of Kotlin (Berlin JUG 16 Apr 2015)
intelliyole
 
Java 8 Streams & Collectors : the Leuven edition
Java 8 Streams & Collectors : the Leuven editionJava 8 Streams & Collectors : the Leuven edition
Java 8 Streams & Collectors : the Leuven edition
José Paumard
 
TclOO: Past Present Future
TclOO: Past Present FutureTclOO: Past Present Future
TclOO: Past Present Future
Donal Fellows
 

Tendances (20)

Clojure for Java developers - Stockholm
Clojure for Java developers - StockholmClojure for Java developers - Stockholm
Clojure for Java developers - Stockholm
 
Optimizing Tcl Bytecode
Optimizing Tcl BytecodeOptimizing Tcl Bytecode
Optimizing Tcl Bytecode
 
Making an Object System with Tcl 8.5
Making an Object System with Tcl 8.5Making an Object System with Tcl 8.5
Making an Object System with Tcl 8.5
 
Kotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime PerformanceKotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime Performance
 
Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2
 
Adventures in TclOO
Adventures in TclOOAdventures in TclOO
Adventures in TclOO
 
JDK1.7 features
JDK1.7 featuresJDK1.7 features
JDK1.7 features
 
Feel of Kotlin (Berlin JUG 16 Apr 2015)
Feel of Kotlin (Berlin JUG 16 Apr 2015)Feel of Kotlin (Berlin JUG 16 Apr 2015)
Feel of Kotlin (Berlin JUG 16 Apr 2015)
 
JavaOne 2013 - Clojure for Java Developers
JavaOne 2013 - Clojure for Java DevelopersJavaOne 2013 - Clojure for Java Developers
JavaOne 2013 - Clojure for Java Developers
 
Javascript development done right
Javascript development done rightJavascript development done right
Javascript development done right
 
The TclQuadcode Compiler
The TclQuadcode CompilerThe TclQuadcode Compiler
The TclQuadcode Compiler
 
JDBC Core Concept
JDBC Core ConceptJDBC Core Concept
JDBC Core Concept
 
#5 (Remote Method Invocation)
#5 (Remote Method Invocation)#5 (Remote Method Invocation)
#5 (Remote Method Invocation)
 
Java 8 Streams & Collectors : the Leuven edition
Java 8 Streams & Collectors : the Leuven editionJava 8 Streams & Collectors : the Leuven edition
Java 8 Streams & Collectors : the Leuven edition
 
TclOO: Past Present Future
TclOO: Past Present FutureTclOO: Past Present Future
TclOO: Past Present Future
 
ES6 in Real Life
ES6 in Real LifeES6 in Real Life
ES6 in Real Life
 
Kotlin: a better Java
Kotlin: a better JavaKotlin: a better Java
Kotlin: a better Java
 
Programming with Python and PostgreSQL
Programming with Python and PostgreSQLProgramming with Python and PostgreSQL
Programming with Python and PostgreSQL
 
The Ring programming language version 1.7 book - Part 30 of 196
The Ring programming language version 1.7 book - Part 30 of 196The Ring programming language version 1.7 book - Part 30 of 196
The Ring programming language version 1.7 book - Part 30 of 196
 
Pattern Matching in Java 14
Pattern Matching in Java 14Pattern Matching in Java 14
Pattern Matching in Java 14
 

Similaire à Nice to meet Kotlin

Java осень 2012 лекция 2
Java осень 2012 лекция 2Java осень 2012 лекция 2
Java осень 2012 лекция 2
Technopark
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)
Alok Kumar
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and Utilities
Pramod Kumar
 

Similaire à Nice to meet Kotlin (20)

JDK 8
JDK 8JDK 8
JDK 8
 
3. Объекты, классы и пакеты в Java
3. Объекты, классы и пакеты в Java3. Объекты, классы и пакеты в Java
3. Объекты, классы и пакеты в Java
 
A topology of memory leaks on the JVM
A topology of memory leaks on the JVMA topology of memory leaks on the JVM
A topology of memory leaks on the JVM
 
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
 
JEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with JavassistJEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with Javassist
 
Java осень 2012 лекция 2
Java осень 2012 лекция 2Java осень 2012 лекция 2
Java осень 2012 лекция 2
 
Wien15 java8
Wien15 java8Wien15 java8
Wien15 java8
 
Why Spring <3 Kotlin
Why Spring <3 KotlinWhy Spring <3 Kotlin
Why Spring <3 Kotlin
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)
 
Java byte code in practice
Java byte code in practiceJava byte code in practice
Java byte code in practice
 
Scala - en bedre Java?
Scala - en bedre Java?Scala - en bedre Java?
Scala - en bedre Java?
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
 
Basic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIBasic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time API
 
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
 
AJUG April 2011 Cascading example
AJUG April 2011 Cascading exampleAJUG April 2011 Cascading example
AJUG April 2011 Cascading example
 
JUnit 5
JUnit 5JUnit 5
JUnit 5
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and Utilities
 
Kotlin
KotlinKotlin
Kotlin
 
JavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programmingJavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programming
 
Scala in practice
Scala in practiceScala in practice
Scala in practice
 

Plus de Jieyi Wu

Plus de Jieyi Wu (10)

Design pattern advanced ii with testing
Design pattern advanced ii with  testingDesign pattern advanced ii with  testing
Design pattern advanced ii with testing
 
Design pattern advanced i
Design pattern advanced iDesign pattern advanced i
Design pattern advanced i
 
Dependency injection
Dependency injectionDependency injection
Dependency injection
 
Application architecture pattern
Application architecture patternApplication architecture pattern
Application architecture pattern
 
Reactive X introduction part1
Reactive X introduction part1Reactive X introduction part1
Reactive X introduction part1
 
Karitoke's supported technology
Karitoke's supported technologyKaritoke's supported technology
Karitoke's supported technology
 
Design pattern - part 3
Design pattern - part 3Design pattern - part 3
Design pattern - part 3
 
Design pattern part 2 - structural pattern
Design pattern part 2 - structural patternDesign pattern part 2 - structural pattern
Design pattern part 2 - structural pattern
 
Design pattern - part 1
Design pattern - part 1Design pattern - part 1
Design pattern - part 1
 
Object-oriented programming
Object-oriented programmingObject-oriented programming
Object-oriented programming
 

Dernier

UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
rknatarajan
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 

Dernier (20)

Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 

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
  • 17.
  • 18.
  • 19. thanks! Any questions? You can find me at @Jieyi jieyi.wu@colverlan.jp