Just look through the basic grammers of Kotlin. To conclude, Kotlin
seems so like Java, but has a lot of additional grammar sugar.
ep01
Why you should learn Kotlin?
- Complete null safety
- Use all Java libraries in Kotin, too
- Kotlin has coroutines
ep02 ep03
ep04
Because the tutorials are too basic, then I switch to official
guideline.
Package definition and
imports
Program entry point
Print to the standard output
Functions
按:Unit
似乎和 Java 的 void
比较像。Unit
可以省略。
Variables
Creating classes and
instances
To define a class, use the class
keyword.
Properties of a class can be listed in its declaration or body.
The default constructor with parameters listed in the class
declaration is available automatically.
Inheritance between classes is declared by a colon (:
).
Classes are final by default; to make a class inheritable, mark it as
open
.
Just like most modern languages, Kotlin supports single-line (or
end-of-line) and multi-line (block) comments.
Block comments in Kotlin can be nested.
String templates
Conditional expressions
for loop
while loop
when expression
Ranges
Check if a number is within a range using in
operator.
Check if a number is out of range.
Iterate over a range.
Or over a progression.
Collection
Check if a collection contains an object using in
operator.
Using lambda expressions to filter and map collections:
Nullable values and null
checks
A reference must be explicitly marked as nullable when
null
value is possible. Nullable type names have
?
at the end.
Return null
if str
does not hold an
integer:
Use a function returning nullable value:
Type checks and automatic
casts
References:
- https://www.youtube.com/channel/UCKNTZMRHPLXfqlbdOI7mCkg
- https://kotlinlang.org/docs/basic-syntax.html