Introduction
- Kotlin is a modern but already mature programming language designed to make developers happier.
- Itβs concise, safe, interoperable with Java and other languages, and provides many ways to reuse code between multiple platforms for productive programming.
Read more about Kotlin from the official documentation This is similar to Java and other programming languages like TypeScript-JavaScript
. If you are familiar with Java or TypeScript, you will find Kotlin syntax very similar.
Package definition
Read more about packages from the official documentation
- Package definition is the first statement in a Kotlin file.
- It is optional. Think of it as a
namespace
in C#
or a module
in TypeScript
.
1
2
3
4
5
6
| package org.example
fun printMessage() { /*...*/ }
class Message { /*...*/ }
// ...
|
- All the contents, such as
classes
and functions
, of the source file are included in this package. - So, in the example above, the full name of
printMessage()
is org.example.printMessage
, and - the full name of
Message
is org.example.Message
.
If the package is not specified, the contents of such a file belong to the default package
with no name.
Import
- Several packages are imported by default.
- Apart from the
default imports
, each file may contain its own import directives
. - We can import a single class or function:
1
| import org.example.Message // Message is now accessible without qualification
|
- Or all the contents of a scope: package, class, object, and so on:
1
| import org.example.* // everything in 'org.example' becomes accessible
|
- If there is a name clash, you can disambiguate by using as keyword to locally rename the clashing entity:
1
2
| import org.example.Message // Message is accessible
import org.test.Message as TestMessage // TestMessage stands for 'org.test.Message'
|
Entry Point
In React, we have index.js
as the entry point. In Kotlin, we have main
function as the entry point.
1
2
3
| fun main() {
println("Hello, World!")
}
|
- The
main
function is the entry point of a Kotlin application. - It is the starting point of the program execution.
- The
main
function can also have arguments.
1
2
3
| fun main(args: Array<String>) {
println(args.contentToString())
}
|
- The
args
parameter is an array of String
arguments passed to the program. - The
Array<String>
is the type of the args
parameter. println
is a function that prints the argument to the standard output.
Functions
Read more about functions from the official documentation
- Functions are declared using the
fun
keyword. - Functions can have parameters and return types.
- The return type is specified after the function name.
1
2
3
| fun sum(a: Int, b: Int): Int {
return a + b
}
|
- A function body can be an expression. Its return type is inferred.
1
2
3
4
5
| fun sum(a: Int, b: Int) = a + b
fun main() {
println("sum of 19 and 23 is ${sum(19, 23)}")
}
|
- A function that returns no meaningful value returns
Unit
. It is optional to specify the return type.
1
2
3
4
5
6
7
| fun printSum(a: Int, b: Int): Unit {
println("sum of $a and $b is ${a + b}")
}
fun main() {
printSum(-1, 8)
}
|
Variables
- Variables are declared using
var
or val
keyword. var
is mutable and val
is immutable.(like let
and const
in JavaScript)- Kotlin is statically typed language, but it can
infer
the type of the variable from the value assigned to it.
1
2
3
4
5
6
7
8
9
10
11
| var name: String = "John"
val age: Int = 25
val isMarried= false // Type inference (kotlin will know that it is a boolean)
fun main() {
val a: Int = 1 // immediate assignment
val b = 2 // `Int` type is inferred
val c: Int // Type required when no initializer is provided
c = 3 // deferred assignment
println("a = $a, b = $b, c = $c")
}
|
Naming Convention
- Variable names should be in camelCase.(e.g.
myVariable
)
Classes
- Classes are declared using the
class
keyword. - Properties can be declared in the class body.
1
2
3
| class Rectangle(val height: Double, val length: Double) {
val perimeter = (height + length) * 2
}
|
- The default constructor with parameters listed in the class declaration is available automatically.
1
2
3
4
5
6
7
| class Rectangle(val height: Double, val length: Double) {
val perimeter = (height + length) \* 2
}
fun main() {
val rectangle = Rectangle(5.0, 2.0)
println("The perimeter is ${rectangle.perimeter}")
}
|
1
2
3
4
5
| open class Shape
class Rectangle(val height: Double, val length: Double): Shape() {
val perimeter = (height + length) * 2
}
|
- Single-line comments start with
//
. - Multi-line comments are enclosed in
/*
and */
.
1
2
3
4
5
6
| // This is a single-line comment
/*
* This is a
* multi-line comment
*/
|
- Block comments can be nested.
1
2
3
| /* The comment starts here
/* contains a nested comment */
and ends here. */
|