DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Stone
Stone

Posted on

kotlin learning notes β… β€”β€” basic grammer

Defining constants and variables

We use var keyword as modifier to modify a variable, and use val keyword modifier to modify a non variable. it needs to be noted here that val is defined as a non variable rather than a constant. There is a difference between the two. You can compare the variables modified by val to the final in Java, which will be better understood.

var age: Int = 10
val greet String = "Hello world"

Both constants and variables can have no initialization value, but they must be initialized before they are referenced
The compiler supports automatic type determination, that is, the compiler can determine the type without specifying the type.
It should be noted here that kotlin is very intelligent. If your variable type can be inferred by the program, it doesn't matter if you don't write the variable type. For example, the number above is assigned to 10. Since this 10 can be inferred to be an int type, and "Hello world" can also be inferred to be a string type, the two can be written as follows:

var age = 10
val greet = "Hello world"

Null checking mechanism

In kotlin, there is a concept of null security. As long as the variable is decorated with a question mark "?", then the variable is declared as a variable that can be null. If it is not decorated with? The program will default to be null. Once it is null, the editor will report an error before it is compiled and run, resulting in the failure of compilation, and it will not be found until the program runs down.

// with? Indicates that it can be null
var age: String? = "23" 
//Throw NullPointerException
val ages = age!!.toInt()
//return null without processing
val ages1 = age?.toInt()
//if age is null, return -1
val ages2 = age?.toInt() ?: -1

note:
!! Indicates that the object is executed without being null

Alt Text

function

It's very simple to declare a function. You just need to use fun keyword to decorate it, followed by the name of the function, and the parameters of the function are in brackets. Note here that the writing of the parameters is the same as that of the previously declared variables.

fun greeting(str:greet):String{
    return greet;
}

String template $

$ represents a variable name or value
$varname represents the value of the variable
${varName.fun ()} represents the method return value of the variable:

var a = 1
val s1 = "a is $a" 

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

range expression

The range expression is formed by the rangeTo function with the operator form.. supplemented by in and !In.
Here are some examples of using range expression:

for (i in 1..4) print(i) // β€œ1234”
for (i in 4..1) print(i) // nothing output

if (i in 1..10) { // equate to 1 <= i && i <= 10
    println(i)
}

// Use step to specify the step size
for (i in 1..4 step 2) print(i) // β€œ13”

for (i in 4 downTo 1 step 2) print(i) // β€œ42”

//Use the until to exclude the end element
for (i in 1 until 10) {   // i in [1, 10) exclude 10
     println(i)
}

Top comments (0)

Super Useful CSS Resources

>> Check out this classic DEV post <<