The world is going crazy. They say that all new mobile projects on Android are written exclusively on Kotlin. Nowadays, it is very dangerous not to learn new technologies. Initially, your knowledge becomes obsolete, you take off from work, live near a heating main, fight with homeless people for food and die in obscurity without learning a functional programming. Therefore, I went to Kurslin to study the course of Kotlin for Java Developers and began to read a book (hello, abreslav , yole ), asked around friends you know how and came back with a certain emptiness in my heart. Help Oleg the traveler find meaning in Kotlin!
โ Java , . a = b
โ , a[1] = 2
โ . - . IDE . IDE , , .
โ API , - map/filter , . . , IDE โ .
โ , IDE. Kotlin IntelliJ IDEA? , Java? . , - JB .
โ it
, . - seq.map { it -> foo(it, 1); }.map { it -> bar(it, 2); }.filter { it -> it.getBaz() > 0; }
. ? ! ยซ , , , ยป.
โ ?.let { foo(it); }?.let { bar(it); }
โ . , . if. .
โ . JvmStatic JvmName, .
, :
class C {
companion object {
@JvmStatic fun foo() {}
fun bar() {}
}
}
, . :
C.foo();
โC.bar();
โ ,C.Companion.foo()
; โC.Companion.bar();
โ? , . , , , :
fun List<String>.filterValid(): List<String>
fun List<Int>.filterValid(): List<Int>
JVM : filterValid(Ljava/util/List;)Ljava/util/List;
:
fun List<String>.filterValid(): List<String>
@JvmName("filterValidInt")
fun List<Int>.filterValid(): List<Int>
: Kotlin checked exceptions. Java- . ยซ ยป @Throws
:
@Throws(IOException::class)
fun foo() {
throw IOException()
}
, ยซ , ยป. , ?
, Java-to-Kotlin Interop , .
โ / get (, ENGLISH? -) โ .
import java.util.Calendar
fun calendarDemo() {
val calendar = Calendar.getInstance()
if (calendar.firstDayOfWeek == Calendar.SUNDAY) { // call getFirstDayOfWeek()
calendar.firstDayOfWeek = Calendar.MONDAY // call setFirstDayOfWeek()
}
if (!calendar.isLenient) { // call isLenient()
calendar.isLenient = true // call setLenient()
}
}
โ - , .
, . , ยซ ยป, โ . MutableList
swap
:
fun MutableList<Int>.swap(index1: Int, index2: Int) {
val tmp = this[index1] // 'this'
this[index1] = this[index2]
this[index2] = tmp
}
val lst = mutableListOf(1, 2, 3)
lst.swap(0, 2) // 'this' 'swap()' 'lst'
- , , , . - . , : , , ? -, ?
, - . , , -.
โ . , reduce.
reduce:
listOf(1, 2, 3).reduce { sum, element -> sum + element } == 6
identity (fold), .
listOf(1, 2, 3).fold(0) { sum, element -> sum + element } == 6
, -? , .
, fold reduce , fold , reduce . , identity .
? - Optional
, ? null , null-friendly .
โ . , -, ? .
, - :
data class User(val name: String, val age: Int)
val duncan = User("Duncan MacLeod", 426)
val (name, age) = duncan
println("$name, $age years of age") // "JaDuncan MacLeodne, 426 years of age"
:
val (name, age) = Pair("Java", 23)
println("$name, $age years of age") // "Java, 23 years of age"
, :
public data class Pair<out A, out B>(
public val first: A,
public val second: B
)
, , . , - . , , .
โ โ ( ).
C++, . , โ . , C++ return , - . , undefined behavior. , . โ . , . .
, Kotlin . , . a
b
, c
, when
, d
, e
f
, !
fun a(check: Int) = b(check)
fun b(check: Int) = c(check)
fun c(check: Int) =
when (check) {
1 -> d()
2 -> e()
else -> f()
}
fun d() = "result 1";
fun e() = "result 2";
fun f() = "result 3";
fun main(args: Array<String>) {
println(::a.returnType)
for (i in 1..3) println(a(i).javaClass.name)
, , . f
, , , .
:
kotlin.String
java.lang.String
java.lang.String
java.lang.String
:
fun d() = "1";
fun e() = 100500;
fun f() = listOf<String>();
kotlin.Any
java.lang.String
java.lang.Integer
kotlin.collections.EmptyList
API. API , Kotlin .
, . , , , . , Kotlin- :-)
Joker 2018 ( ), (asm0dey) , Kotlin ( ), , GraalVM, Spring, Spring Security, Spring Transactions, jOOQ, ..
Kotlin Java ? . , Kotlin . !
. , 8-9 2018, Mobius. JetBrains , Kotlin Muplitplatform. , , Kotlin, , . , , , , . .
Source: https://habr.com/ru/post/431678/
All Articles