Kotlin language basics (1) Kotlin language features

Kotlin language overview (1) - language features

Kotlin is a high-level strongly typed language that can run on the Java VM.
It has the following characteristics:

1. The code is concise, the amount of code is small, and it is easy to learn and understand.
For example:

/**
 * 定义数据类Student
 * @property id String 学号
 * @property name String 姓名
 * @property gender String 性别
 * @constructor
 */
data class Student(val id:String,val name:String,val gender:String)

The code will automatically generate hashCode(), equals(), toString() and other methods corresponding to the data class Student, without the need for a program to write code to complete.

2.Kotlin has good security.

Kotlin provides some features to avoid runtime crashes. For example:
automatically detect whether the object is null to avoid the problem of null pointer exception.
Provide safe access to objects, such as safe calls, non-null assertion operations, and nullable operators to handle nullable objects differently.

3.Kotlin is based on Java VM language

Kotlin is based on the Java VM language and can interact with Java code and use Java class libraries directly. Applications in any Java environment ecosystem can be developed.

import java.util.Scanner
/*
 * Main
 * 定义主函数
 */
fun main(){
    
    
    //定义Scanner对象
    val sc = Scanner(System.`in`)
    println("输入你的名字:")
    var name = sc.next()
    println("你好!${name}欢迎来到Android世界!")
    name = sc.next()
    println("你好!欢迎 $name 新世界!")
}

4. Kotlin supports functional programming.
Kotlin language supports functional programming, and less boilerplate code can easily transfer code blocks. The code will be simpler. For example:
traditional approach:

import java.util.function.Consumer

fun main(){
    
    
    val s=setOf("hello","kotlin","language","world")
    //传统的做法
    //创建接口Consumer匿名对象
    val consumer:Consumer<String> = object:Consumer{
    
    
        override fun accept(t: String) {
    
    
           println(t)
        }
    }
    //调用
    s.forEach(consumer)
}

Using functional programming:
the full way:

fun main(){
    
    
    val s=setOf("hello","kotlin","language","world")
    //函数式编程的处理方式
    s.forEach{
    
    it:String->
        println(it)
    }
}

Because the Consumer interface has only one method accept, and accept has only one parameter, the simplified Lambda method is:

fun main(){
    
    
    val s=setOf("hello","kotlin","language","world")
    //函数式编程的处理方式
    s.forEach{
    
    
        println(it)
    }
}

The running functions of the above two code snippets are exactly the same, and the running results are as follows:
hello
kotlin
language
world

If you need to filter and print out the string containing "o", the code is as follows:

fun main(){
    
    
    //定义字符串集合  
    val s=setOf("hello","kotlin","language","world")
    //筛选除包含”o"字符的字符串,并打印输出
    println(s.filter{
    
    it.contains("o")})
}

Running result:
[hello, kotlin, world]

5.Kotlin multi-platform sharing

Kotlin code can be shared across multiple platforms, including Android, iOS, web, and native.
Taking Android Studio as an example, you only need to install the Kotlin MultiplePlatform Mobile plug-in in Android Studio, as shown in the figure below.
Android Studio installed KMM
Figure 1
can realize the sharing of a Kotlin code under different platforms.
For example: Start Android Studio
to create a new project, select Kotlin Multiplatform App, as shown in Figure 2:
Create a new Kotlin multi-platform application project
Figure 2
Insert image description here
Figure 3 Continue to set the relevant parameters of the new project
Configure multi-platform parameters
Figure 4 Set the application name and other parameters of the new project
Click "Finish"
Insert image description here

Figure 5 Project hierarchy.
For details, please refer to
Get started with Kotlin Multiplatform for mobile

Guess you like

Origin blog.csdn.net/userhu2012/article/details/132642360