森林里有一个移动开发人员,看到-Kotlin着火了。 他坐在科特林被烧死

世界快疯了。 他们说,Android上的所有新移动项目都是完全用Kotlin编写的。 如今,不学习新技术是非常危险的。 刚开始,您的知识变得过时,您失业而去,住在暖气总管附近,与无家可归的人争夺食物,死于默默无闻,而没有学习功能编程。 因此,我去了Kurser,学习了Kotlin for Java Developers课程,并开始读一本书 (嗨, abreslavyole ),我问我的朋友您哪里有自己的了解 ,然后我的内心有些空虚。 帮助旅行者奥列格(Oleg)在Kotlin中寻找意义!


  • 奖励:habro问题“您如何使用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/zh-CN431678/


All Articles