Article source: www.toutiao.com/i6837300065…

Kotlin profile

Kotlin is a cross-platform static programming language developed by JetBrains. It is currently managed by the Kotlin Foundation, a joint venture between JetBrains and Google. Kotlin can seamlessly interact with Java code and is the preferred language for Android development. Brief development history:

  • 2011.07 JetBrains launched a new JVA-BASED language :Kotlin. Kotlin takes its name from the Island of Kotlin, near St. Petersburg, Russia.
  • 2016.02 Kotlin released the first stable release: V1.0
  • Google announced Kotlin as a Level 1 Support language for Android development at the I/O conference in May 2017.
  • 2017.11 Kotlin supports compiling to JavaScript.
  • Kotlin introduced coroutines to simplify asynchronous programming.
  • In May 2019, Google announced Kotlin as the preferred language for Android development at I/O.

Variable Definition (constant)

  1. Variable definition var Variable name [: type] [= initial value] Example:
var a: Int = 1
var b = 1   // The system automatically concludes that the variable type is Int
Copy the code
  1. Definition of an immutable variable (cannot be modified after being defined) val Name of the variable [: type] [= initial value] Example:
val a: Int = 1
val b = 1   // The system automatically concludes that the variable type is Int
Copy the code

Underlying data types

  1. Boolean Value Boolean values can be true or false. Example:
var flag: Boolean = true
Copy the code
  1. Digital type
type Bit width
Byte 8
Short 16
Int 32
Long 64
Double 64
Float 32

Sample:

var i: Byte = 1
var j: Short = 1
var k: Int = 1
var l: Long = 1
var m: Double = 1.0
var n: Float = 1f
Copy the code
  1. character
var char:Char='a'
Copy the code
  1. String type
var name: String = "jack"
Copy the code

Under controlled conditions

  • If – else
var name = Anonymous
if (name == "Sun Wukong") {
    print("The risk")}else {
    print("Human")}Copy the code
  • S the way
var name = Anonymous
when (name) {
    "Sun Wukong" -> print("The risk")
    else -> print("Human")}Copy the code

Cycle control

  • The for loop
var list = listOf(1.2.3)
for (item: Int in list) {
    println(item)
}
Copy the code
for (item: Int in 1.10.) {
    println(item)
}

Copy the code
  • The while loop
var value = 5
while (value > 0) {
    println( value--)
}
Copy the code
  • The do while loop
var value = 5
do {
    println(value--)
} while (value > 0)
Copy the code

function

class

The Kotlin class can contain: constructors, initializer code, functions, attributes, and inner classes. Sample:

  • The empty class
class User
Copy the code
  • Classes with attributes
class User() {
    var name: String = "Empty."
}
Copy the code
  • Contains attributes and default constructors
class User(var name:String)
Copy the code
  • Data classes automatically add toString(), hashCode(), equals(), and copy() methods
data class User(var name:String)
Copy the code
  • Class in the constructor
class User(var name: String) {
    init {
        println("Initialize in constructor")}}Copy the code
  • multiconstructor
class User(var name: String, var age: Int.var gender: String) {
    init {
        print("Default constructor")}constructor(name: String, age: Int) : this(name, age, "M") {
        print("Another constructor")}constructor(name: String) : this(name, 18) {
        print("Let's have another constructor.")}}Copy the code

interface

The interface keyword declaration supports defining methods (which can have default implementations) and properties.

  • Defines the interface
interface MyInterface {
    fun test1(a)    / / unrealized
    fun test2(a) {  / / has been realized
        // Optional method body
        println("Ha ha")}// Can be overridden
    val name: String
}
Copy the code
  • Class implementation interface
class Student(override val name: String) : Exam {
    override fun exam1(a) {
        println("${name}Retake")}}Copy the code

annotation

  • Single-line comments, using //
// Single-line comment
Copy the code
  • Multi-line comments, using /* */
/* Multi-line comments */
Copy the code
  • For document comments, use /** */
/** **
Copy the code

Android development tutorial series summary

Android development history and prospects

Android development history Outlook of Android development

A preliminary study on the android

Install developer tools to create your first Android project

Development language learning

Kotlin language basics

UI control learning series

UI control _TextView UI control _EditText UI control _Button UI control _ImageView UI control _RadioButton UI control _CheckBox UI control _ProgressBar

Follow the headlines to get the latest articles: