BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
3 kotlin vs. java- what kotlin has that java does not
1.
2.
3.
4. 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
5. 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 }
6. 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
7. 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 {
// ...
}
10. 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
11. 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'
12. 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") }
13.
14. if (obj is String) {
print(obj.length)
}
if (obj !is String) { // same as !(obj is String)
print("Not a String")
}
else {
print(obj.length)
}
15. "Unsafe"
val x: String = y as String
val x: String? = y as String?
"Safe" (nullable)
val x: String? = y as? String
16.
17. The primary constructor is part of the class header: it goes after the class name
(and optional type parameters)
class Person constructor(firstName: String) {
}
18. 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")
}
}
19.
20. class A {
fun foo() { }
}
class B {
fun call() { }
}
class C {
var b = B()
fun call() {
return b.call()
}
}
21. 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
}
22.
23. 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)
}
24.
25. 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)
}
}
26.
27. 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