Kotlin environment installation and syntax tutorial

table of Contents

Kotlin tutorial

My first Kotlin program

Minimal version

Object-oriented

Why choose Kotlin?

Reference link

Kotlin tutorial

 

Kotlin is a statically typed programming language that runs on the Java virtual machine, known as the Swift of the Android world, designed and developed by JetBrains and open sourced.

Kotlin can be compiled into Java bytecode or JavaScript, which is convenient to run on devices without JVM.

In Google I/O 2017, Google announced Kotlin as the official development language for Android.

My first Kotlin program

Kotlin program files end with .kt, such as hello.kt and app.kt.

Minimal version

package hello // optional package header fun main(args: Array<String>) {// package-level visible function, accepts an array of strings as a parameter println("Hello World!") // semicolon can be omitted}

package hello                      //  可选的包头
 
fun main(args: Array<String>) {    // 包级可见的函数,接受一个字符串数组作为参数
   println("Hello World!")         // 分号可以省略
}

Object-oriented

class Greeter(val name: String) {fun greet() {println("Hello, $name")}} fun main(args: Array<String>) {Greeter("World!").greet() // Create An object does not need the new keyword}

class Greeter(val name: String) {
   fun greet() { 
      println("Hello, $name")
   }
}
 
fun main(args: Array<String>) {
   Greeter("World!").greet()          // 创建一个对象不用 new 关键字
}

Why choose Kotlin?

  • Simplicity: Greatly reduce the amount of boilerplate code.
  • Security: Avoid the entire class of errors such as null pointer exceptions.
  • Interoperability: Make full use of the existing libraries of JVM, Android and browsers.
  • Tool-friendly: It can be built using any Java IDE or using the command line.

Reference link

Guess you like

Origin blog.csdn.net/boonya/article/details/111050221