Expressiveness/Conciseness
Safety
Portability/Compatibility
Convenience
High Quality IDE Support
Community
Android 👀
More than a gazillion devices run Java Kotlin
Kotlin is named after an island in the Gulf of Finland., Kotlin Island
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?
xxxxxxxxxx
fun main(args: Array<String>) {
println("Hello, world!")
}
fun main() {
println("Hello, world!")
}
fun main() = println("Hello, world!")
We might as well give running the following code snippets a try.
xxxxxxxxxx
fun main(args: Array<String>) {
println("Hello, world!");
}
fun main() {
println("Hello, world!");
}
fun main() = println("Hello, world!");
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.
This is also a code that outputs Hello, World!
.
xxxxxxxxxx
fun main(args: Array<String>) {
print("Hello")
println(", world!")
}
Through this piece of code, we can learn a lot
An entry point of a Kotlin application is the main top-level function.
It accepts a variable number of String arguments that can be omitted.
print prints its argument to the standard output.
println prints its arguments and adds a line break.
By observing this main
function, we can identify the fundamental structure of a simple function:
xxxxxxxxxx
fun functionName(parameter1: Type) {
// Function name | Parameters passed | Parameter types
// Function body
}
print
and println
are both standard library functions used for outputting text. The difference lies in the fact that println
adds a newline character.
The following code defines two values in Kotlin:
xxxxxxxxxx
var a = 1
val B = 12
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.
xxxxxxxxxx
val A = 12
a = 13 // Error:Val cannot be reassigned
var b = 12
b = 13 // Running !
Specify the type of the constant/variable as Int; if not specified, it will be automatically inferred.
xxxxxxxxxx
val A: Int = 12
var b: Int = 12
Certainly, you can specify the type first and then assign a value on the next line.
xxxxxxxxxx
val A: Int
A = 12
var b: Int
b = 12