Se ha denunciado esta presentación.
Se está descargando tu SlideShare. ×

3 kotlin vs. java- what kotlin has that java does not

Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Cargando en…3
×

Eche un vistazo a continuación

1 de 27 Anuncio
Anuncio

Más Contenido Relacionado

Presentaciones para usted (20)

Similares a 3 kotlin vs. java- what kotlin has that java does not (20)

Anuncio

Más reciente (20)

3 kotlin vs. java- what kotlin has that java does not

  1. 1. A function that is not declared, but passed immediately as an expression: max(strings, { a, b -> a.length < b.length }) As a function, it is equivalent to: fun compare(a: String, b: String): Boolean = a.length < b.length
  2. 2. fun <T> max(collection: Collection<T>, less: (T, T) -> Boolean): T? { var max: T? = null for (it in collection) if (max == null || less(max, it)) max = it return max } Lambda Expression Syntax: val sum = { x: Int, y: Int -> x + y }
  3. 3. Using higher-order functions imposes certain runtime penalties: ● each function is an object ● it captures a closure Memory allocations (both for function objects and classes) and virtual calls introduce runtime overhead
  4. 4. This kind of overhead can be eliminated by inlining the lambda expressions lock(l) { foo() } l.lock() try { foo() } finally { l.unlock() } inline fun <T> lock(lock: Lock, body: () -> T): T { // ... }
  5. 5. inline fun foo(inlined: () -> Unit, noinline notInlined: () -> Unit) { // ... }
  6. 6. Kotlin, similar to C#, provides the ability to extend a class with new functionality without having to inherit from the class or use any type of design pattern such as Decorator This is done via special declarations called extensions Kotlin supports extension functions and extension properties
  7. 7. fun MutableList<Int>.swap(index1: Int, index2: Int) { val tmp = this[index1] // 'this' corresponds to the list this[index1] = this[index2] this[index2] = tmp } val l = mutableListOf(1, 2, 3) l.swap(0, 2) // 'this' inside 'swap()' will hold the value of 'l'
  8. 8. open class C class D: C() fun C.foo() = "c" fun D.foo() = "d" fun printFoo(c: C) { println(c.foo()) } printFoo(D()) class C { fun foo() { println("member") } } fun C.foo() { println("extension") } class C { fun foo() { println("member") } } fun C.foo(i: Int) { println("extension") }
  9. 9. if (obj is String) { print(obj.length) } if (obj !is String) { // same as !(obj is String) print("Not a String") } else { print(obj.length) }
  10. 10. "Unsafe" val x: String = y as String val x: String? = y as String? "Safe" (nullable) val x: String? = y as? String
  11. 11. The primary constructor is part of the class header: it goes after the class name (and optional type parameters) class Person constructor(firstName: String) { }
  12. 12. The primary constructor cannot contain any code Initialization code can be placed in initializer blocks, which are prefixed with the init keyword class Constructors { init { println("Init block") } constructor(i: Int) { println("Constructor") } }
  13. 13. class A { fun foo() { } } class B { fun call() { } } class C { var b = B() fun call() { return b.call() } }
  14. 14. interface Base { fun print() } class BaseImpl(val x: Int) : Base { override fun print() { print(x) } } class Derived(b: Base) : Base by b fun main(args: Array<String>) { val b = BaseImpl(10) Derived(b).print() // prints 10 }
  15. 15. if (i in 1..10) { // equivalent of 1 <= i && i <= 10 println(i) } for (i in 1..4) print(i) // prints "1234" for (i in 4..1) print(i) // prints nothing for (i in 4 downTo 1) print(i) // prints "4321" for (i in 1..4 step 2) print(i) // prints "13" for (i in 4 downTo 1 step 2) print(i) // prints "42" for (i in 1 until 10) { // i in [1, 10), 10 is excluded println(i) }
  16. 16. Functions that overload operators need to be marked with the operator modifier Unary prefix operators Expression Translated to +a a.unaryPlus() -a a.unaryMinus() !a a.not() data class Point(val x: Int, val y: Int) operator fun Point.unaryMinus() = Point(-x, -y) val point = Point(10, 20) println(-point) // prints "(-10, -20)" data class Counter(val dayIndex: Int) { operator fun plus(increment: Int): Counter { return Counter(dayIndex + increment) } }
  17. 17. data class User(val name: String, val age: Int) The compiler automatically derives the following members from all properties declared in the primary constructor: equals()/hashCode() pair toString() of the form "User(name=John, age=42)" componentN() functions corresponding to the properties in their order of declaration copy() function

×