- Discuss Java pros and cons
- Kool features in Kotlin.
- How to use Kotlin in Android development.
- How to migrate old Android java code to new Kotlin code.
26. Java generic type is
erasure
List<String> list = new ArrayList<String>();
list.add("Hello Google I/O");
String x = list.get(0);
List list = new ArrayList();
list.add("Hi");
String x = (String) list.get(0);
27. public <T>List<T> castCollection(List srcList, Class<T> clas){
List<T> list =new ArrayList<T>();
for (Object obj : srcList) {
if(obj instanceof T){
// processing code here
}
}
return list;
}
28. public <T>List<T> castCollection(List srcList, Class<T> clas){
List<T> list =new ArrayList<T>();
for (Object obj : srcList) {
if(obj instanceof T){
// processing code here
}
}
return list;
}
public <T>List<T> castCollection(List srcList, Class<T> clas){
List<Object> list =new ArrayList<Object>();
for (Object obj : srcList) {
if(obj instanceof Object){
// processing code here
}
}
}
37. JetBrains lead Dmitry Jemerov said that:
Most languages did not have the features they were looking
for, with the exception of Scala. However, he cited the slow
compile time of Scala as an obvious deficiency. One of the
stated goals of Kotlin is to compile as quickly as Java.
(Wikipedia)
65. Higher Order Function
fun request(url: String, work: (url: String) -> Int) {
println("prepare request")
val result = work(url)
println("finish with result code: $result")
}
request("www.google.com", { url ->
println("sending request to $url")
200
})
66. Higher Order Function
fun request(url: String, work: (url: String) -> Int) {
println("prepare request")
val result = work(url)
println("finish with result code: $result")
}
request("www.google.com", { url ->
println("sending request to $url")
200
})
prepare request
sending request to www.google.com
finish with result code: 200
67. Higher Order Function
val strings = mutableListOf("Google", "I/O", "Extended")
// Find all strings with length > 3 —> [Google, Extended]
strings.filter { it.length > 3 }.sortedBy { it }
68. Higher Order Function
val strings = mutableListOf("Google", "I/O", "Extended")
// sort ascending —> [Extended, Google]
strings.filter { it.length > 3 }.sortedBy { it }
69. Higher Order Function
val strings = mutableListOf("Google", "I/O", "Extended")
// convert to upper case —> [EXTENDED, GOOGLE]
strings.filter { it.length > 3 }.sortedBy { it }.map { it.toUpperCase() }
70. Higher Order Function
val strings = mutableListOf("Google", "I/O", "Extended")
strings.filter { it.length > 3 }.sortedBy { it }.map { it.toUpperCase() }
[EXTENDED, GOOGLE]
71. Higher Order Function
val strings = mutableListOf("Google", "I/O", "Extended")
strings.filter { it.length > 3 }.sortedBy { it }.map { it.toUpperCase() }
[EXTENDED, GOOGLE]
public inline fun <T> Iterable<T>.filter(predicate: (T) -> Boolean): List<T> {
return filterTo(ArrayList<T>(), predicate)
}
72. Higher Order Function
val strings = mutableListOf("Google", "I/O", "Extended")
strings.filter { it.length > 3 }.sortedBy { it }.map { it.toUpperCase() }
[EXTENDED, GOOGLE]
public inline fun <T> Iterable<T>. filter (predicate: (T) -> Boolean): List<T> {
return filterTo(ArrayList<T>(), predicate)
}
79. Operator Overloading
class Point(xCoor: Float, yCoor: Float) {
val x = xCoor
val y = yCoor
operator fun plus(that: Point): Point = Point(x + that.x, y + that.y)
operator fun div(div: Int): Point = Point(x / div, y / div)
fun log() = println("($x,$y)")
}
80. Operator Overloading
class Point(xCoor: Float, yCoor: Float) {
val x = xCoor
val y = yCoor
operator fun plus(that: Point): Point = Point(x + that.x, y + that.y)
operator fun div(div: Int): Point = Point(x / div, y / div)
fun log() = println("($x,$y)")
}
val firstPoint = Point(6f, 5f)
val secondPoint = Point(8f, 3f)
// (7.0, 4.0)
val midPoint = (firstPoint + secondPoint) / 2
midPoint.log()
82. Operator Overloading
class Point(xCoor: Float, yCoor: Float) {
val x = xCoor
val y = yCoor
fun log() = println("($x,$y)")
}
operator fun Point.plus(that: Point): Point = Point(x + that.x, y + that.y)
operator fun Point.div(div: Int): Point = Point(x / div, y / div)
83. Operator Overloading
class Point(xCoor: Float, yCoor: Float) {
val x = xCoor
val y = yCoor
fun log() = println("($x,$y)")
}
operator fun Point.plus(that: Point): Point = Point(x + that.x, y + that.y)
operator fun Point.div(div: Int): Point = Point(x / div, y / div)
val firstPoint = Point(6f, 5f)
val secondPoint = Point(8f, 3f)
// (7.0, 4.0)
val midPoint = (firstPoint + secondPoint) / 2
midPoint.log()
87. Reified type parameter
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(i);
inline fun <reified T : Activity> Activity.startActivity() {
startActivity(Intent(this, T::class.java))
}
88. Reified type parameter
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(i);
inline fun <reified T : Activity> Activity.startActivity() {
startActivity(Intent(this, T::class.java))
}
startActivity<DetailActivity>()
89. Infix Call Function
class Point(xCoor: Float, yCoor: Float) {
val x = xCoor
val y = yCoor
fun multiply(k: Int) = Point(x * k, y * k)
}
val firstPoint = Point(6f, 5f)
firstPoint.multiply(3)
90. Infix Call Function
class Point(xCoor: Float, yCoor: Float) {
val x = xCoor
val y = yCoor
infix fun multiply(k: Int) = Point(x * k, y * k)
}
val firstPoint = Point(6f, 5f)
firstPoint.multiply(3)
91. Infix Call Function
class Point(xCoor: Float, yCoor: Float) {
val x = xCoor
val y = yCoor
infix fun multiply(k: Int) = Point(x * k, y * k)
}
val firstPoint = Point(6f, 5f)
firstPoint.multiply(3)
firstPoint multiply 3
93. SELECT Country.name, COUNT(Customer.id) FROM Country
JOIN Customer
ON Country.id = Customer.country_id
GROUP BY Country.name
ORDER BY COUNT(Customer.id) DESC
LIMIT 1
Expose Library
94. SELECT Country.name, COUNT(Customer.id) FROM Country
JOIN Customer
ON Country.id = Customer.country_id
GROUP BY Country.name
ORDER BY COUNT(Customer.id) DESC
LIMIT 1
(Country join Customer)
.slice(Country.name, Count(Customer.id)).selectAll()
.groupBy(Country.name).orderBy(Count(Customer.id), isAsc = false).limit(1)
Expose Library