Kotlin study notes

Blow a wave first


Kotlin execution process

Variables and constants

Common data types

Hello World case

fun main() {
    // var is used to declare variables; str is the variable name, followed by ":" separated from the variable type; String is a variable type,
    var str: String = "Hello World"

It can be concluded from the above case that we initialize a variable in KT. The format is as follows:

Do you  

  read-only variable

Declare modifiable variables, using the var keyword.
Declare a read-only variable, using the val keyword.

//Read only variables are not absolutely read-only
val str2="I'm a read-only variable"
//str2 = "change the value, ouch, it's wrong!"

Type inference

For initialized variables, kt allows us to omit type definitions.

  Compile-time constant

const val MY_NAME: String = "luffy"
const val MAX: Int = 666
const val DOUBLE: Double = 66.88
const val FLOAT: Float = 66.99F
const val LONG: Long = 2021L
const val SHORT: Short = 2
const val BYTE: Byte = 6
const val CHAR: Char = 'A'
const val BOOLEAN: Boolean = true
fun main() {

Data type of Kotlin  

Kotlin only provides reference data type. For higher performance, kotlin compiler will use basic data type in Java bytecode.

Note: double click shift and enter show kotlin bytecode to view kotlin bytecode. Or tools - > kotlin - > show kotlin bytecode.


if / if else / if else if   Expressions are the same as Java and will not be repeated.

Range expression: in A..B,in keyword is used to check whether a value is within the specified rang e.

const val MONEY: Int = 399
fun main() {
    // Rangs expression, prototype: override fun contains (value: int): Boolean = first < = value & & value < = last
    if (MONEY in 1..100) {
        println("You can reserve a single apartment")
    } else if (MONEY in 101..200) {
        println("You can reserve a king room")
    } else if (MONEY in 201..360) {
        println("You can book a couple business hotel")
    } else {
        println("VIP, if you apply for a card, this consumption is free~")

when expression

Similar to the switch...case statements in C and Java, it means that a certain condition is met and the specified code is executed. As long as the code contains else if, it can be replaced by when expression.

const val school = "university"
fun main() {
    val level = when (school) {
        "kindergarten" -> "child"
        "primary school" -> "children"
        "middle school" -> "juvenile"
        "university" -> "Older children"
        "Work for three or five years" -> "Greasy uncle"
        else -> "Are you afraid of being an immortal~"

String template
① The template supports putting variables in double quotation marks of strings;
② Supports inserting the value of an expression into a string. The expression needs to be inserted into ${}.

fun main() {
    val food = "carrot"
    println("My favorite is $food")
    println("1 Bigger than 2? ${if (1 > 2) "Right" else "incorrect"}")


Define a function


Input parameters are more scientific in front, with input first and output later.

fun main() {
    val sum = getSum(1, 5)

 * private Is a modifier. No modifier is specified. The default is public
 * fun Is the keyword that declares the function
 * getSum Is the function name
 * a and b in parentheses are function parameters respectively, and Int is parameter type
 * Int Is the return value type
private fun getSum(a: Int, b: Int): Int {
    return a + b

Default value parameter

If you do not intend to pass in a value parameter, you can specify a default value for the parameter in advance.

 * If you do not intend to pass in a value parameter, you can specify a default value for the parameter in advance.
 * For example, the second parameter of the function is passed in "Honey" by default
private fun doSomething(firstOne: String, secondOne: String = "Honey") {
    println("$firstOne love $secondOne forever")

Named function parameters

If you use named value parameters, you can ignore the order of value parameters.

    //Named value parameter
    doSomething(secondOne = "Honey",firstOne = "Luffy")

No return value type

Unit type

Not all functions have return values. Functions without return values in kt are called unit functions. Unit is more like void types in Java, which means no return value types, but void cannot explain the modern language feature of generics.

    //kotlin.Unit has no return value type

  Noting type

    //Nothing type: the return value of the built-in function TODO is nothing
    TODO("Don't walk, will you? Run, wow, hahaha, come on, run!")
    println("after TODO")

We can refer to TODO to return Nothing type and terminate our program.

Function name in backquotes

Kotlin can name functions with spaces and special characters, but the function name needs to be enclosed in a bunch of backquotes. This writing method is to support the interoperability between kotlin and Java. Because kotlin and java have different keywords, they cannot be used as function names. Using backquotes can avoid conflicts.

fun main() {

 * Functions for testing special functions
fun `**special function of test special function**`(){


Let's talk so much first. If you want to know what will happen next, let's listen to it next time ~ welcome comments and praise. Everyone's support is very important to me (≥ ◉◡◉≤)

Tags: Java Scala kotlin

Posted on Fri, 10 Sep 2021 04:25:46 -0400 by iblackedout