Introduction to Kotlin

Why Kotlin

Name

Kotlin is named after an island in the Gulf of Finland., Kotlin Island

Hello,World!

In each of the following Kotlin code snippets with distinct writing styles, you'll find they all produce the output Hello, World!. Now, you might be wondering, where are the semicolons?

We might as well give running the following code snippets a try.

All the statements within these function bodies have semicolons at the end, and running them reveals no issues. This is because semicolons are neither mandatory nor prohibited. An expression can end with a semicolon, but it can be omitted if it's separated from the next expression by a newline character.

The basics

This is also a code that outputs Hello, World!.

Through this piece of code, we can learn a lot

By observing this main function, we can identify the fundamental structure of a simple function:

print and println are both standard library functions used for outputting text. The difference lies in the fact that println adds a newline character.

Variables

The following code defines two values in Kotlin:

The var keyword indicates a variable, while the val keyword represents a value.

var denotes a variable, meaning its value can be changed. On the other hand, val represents a constant value that cannot be altered.

Specify the type of the constant/variable as Int; if not specified, it will be automatically inferred.

Certainly, you can specify the type first and then assign a value on the next line.

 

PreviousHOMENext