This is the first day of my participation in the Gwen Challenge in November. Check out the details: the last Gwen Challenge in 2021

Kotlin grammar

1. Program entry

The entrance to Koltin is the same as the main function in Java

fun main() {
    println("Hello world!")
}
Copy the code

Another main function that takes arguments

fun main() {
    prinln("Hi kotlin")
}
Copy the code

2. Function declaration

Instead of declaring a type first and then a variable, Kotlin’s functions declare a variable first and then a type. For example, a summation function is declared as follows:

fun sum(a:Int, b:Int):Int{
    return a+b
}
Copy the code

In addition, the return value type also follows the function name, separated by:. There’s one other detail that I didn’t notice, but Kotlin syntax doesn’t require a semicolon at the end. Cherish the keyboard keys.

Or even, you could just write it as

fun sum(a:Int, b:Int) = a+b
Copy the code

3. Function declaration Unit with no return value

fun printSum(a:Int, b:Int):Unit {
    println("sum of $a and $b is ${a+b}")
}
Copy the code

4. Variable declarations

Constant declarations, once val is used, cannot be modified, similar to final in Java

val a : Int = 1;
val b = 2
val c : Int
    c = 3
Copy the code

Variable declaration var

var x = 4
x = 5
x += 1
Copy the code

5. Classes and instances

Class declaration class example:

    class Rectangle(var height:Double, var width:Double) {
        var perimeter = (height + width) * 2
    }
Copy the code

6. Inheritance

A class that can be inherited needs to be decorated with the keyword open

open class Shape
class Rectangle(var height:Double, var width:Double):Shape(){
    var perimeter = (height + width) * 2
}
Copy the code

7. The string

fun main() {
    var a = 1
    val S1 = "a is $a"

    a = 2
    val s2 = "${s1.replace("is", "was")}, but now is $a"
    println(s2)
}
Copy the code

A was 1, but now is 2

8. Conditional statements

fun maxOf(a:Int, b:Int) {
    if(a > b) {
        return a
    } else {
        return b
    }
}
Copy the code

Or is it

Fun maxOf(a:Int, b:Int) = if (a > b) a else b fun main(){println(" Max of 0 and 42 is ${maxOf(0,42)}")}Copy the code

9. The for loop

val items = listOf("apple", "banana", "kiwifruit")
for (item in items) {
    println(item)
}

val items = listOf("apple", "banana", "kiwifruit")
for (index in items.indices) {
    println("item at $index is ${items[index]}")
}
Copy the code

10. While loop

val items = listOf("apple", "banana", "kiwifruit")
var index = 0
while (index < items.size) {
    println("item at $index is ${items[index]}")
    index++
}
Copy the code

11.when

Similar to switch in Java

fun describe(obj: Any): String = when (obj) { 1 -> "One" "Hello" -> "Greeting" is Long -> "Long" ! is String -> "Not a string" else -> "Unknown" }Copy the code

12. The range Ranges

val x = 10 val y = 9 if (x in 1.. y+1) { println("fits in range") }Copy the code

Collection of 13.

fun main() {
    val items = listOf("apple", "banana", "kiwifruit")
    for (item in items) {
        println(item)
    }
}
Copy the code

14.Nullable and null value detection

fun parseInt(str:String):Int? {
    //...
}
Copy the code

Such as:

fun printProduct(arg1: String, arg2: String) { val x = parseInt(arg1) val y = parseInt(arg2) // Using `x * y` yields error because they may hold nulls. if (x ! = null && y ! = null) { // x and y are automatically cast to non-nullable after null check println(x * y) } else { println("'$arg1' or  '$arg2' is not a number") } }Copy the code

15. Type check is

fun getStringLength(obj: Any): Int? {
    if (obj is String) {
        // `obj` is automatically cast to `String` in this branch
        return obj.length
    }

    // `obj` is still of type `Any` outside of the type-checked branch
    return null
}
Copy the code

or

fun getStringLength(obj: Any): Int? { if (obj ! is String) return null // `obj` is automatically cast to `String` in this branch return obj.length }Copy the code

Or,

fun getStringLength(obj: Any): Int? {
    // `obj` is automatically cast to `String` on the right-hand side of `&&`
    if (obj is String && obj.length > 0) {
        return obj.length
    }

    return null
}
Copy the code

More

Kotlin website

Kotlin making address

The Kotlin Blog