Kotlin Hello World

Kotlin 和 Java 一样,可以在PC上运行,可以用IDEA进行开发,环境配置参考Getting Started with IntelliJ IDEA
官方提供了一个学习网站,可以运行一些例子Kotlin
也可以通过命令行的方式编译运行,参考Working with the Command Line Compiler

PC

fun main(args: Array<String>) {
    println("Hello, World!")
}

如上,是一个最简单的 Hello World。

Android

package com.hello.test

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        textView.text = "Hello World!" // same ass textView.setText()
    }
}

入门

Kotlin代码文件以.kt后缀,源代码所在的目录结构不必与包结构保持一致,但最好和Java一样,目录和包名一致。

  1. Kotlin兼容Java,可以直接调用Java代码;
  2. 与Java相比,行结束不需要分号;
  3. 包定义和import语句跟Java一样;
  4. 变量的定义,” 变量: 类型 “ 的形式,如 savedInstanceState: Bundle?(?表示变量可为null);
  5. 函数定义以 fun 开头, 如 fun test(arg: String): Int;
  6. 类定义以 class开头,如 class MainActivity : AppCompatActivity()。

参考*
Getting Started with IntelliJ IDEA
Working with the Command Line Compiler

    原文作者:jinkui
    原文地址: https://www.jianshu.com/p/4ee0bbc892d2
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞