First Program in Kotlin
On this page (10sections)
Overview
- Kotlin is a general purpose, open source, the statically-typed programming language that runs on the JVM (Java virtual machine) infrastructure.
- It is focused on Concise, simple, very easy to understand, safety, and library support.
- Kotlin is an official language on Android
- Kotlin is a fully supported programming language on Android via Android Studio 3.0
Here, Kotlin first program is a simple program that prints output “Hello, World! - www.littledrops.net” on the screen.
Setting up the Kotlin
- Getting Started with IntelliJ IDEA
- Getting Started with Eclipse
- Working with the Command Line Compiler
Syntax
fun main(args: Array<String>) {
// Main Program Code
}
Simple Kotlin Example Program
// My First Program
fun main(args: Array<String>) {
println("Hello, world! - www.littledrops.net")
}
Sample Output
Hello, world! - www.littledrops.net
How does Kotlin program work?
1. Comment line
// My First Program
The text preceded by // is a comment line in Kotlin (similar to C, C++, and Java). Comments are neglected by Kotlin compiler. Comments are designed for the better understanding of code and functionality and changes in the program.
2. Main Function
fun main(args : Array<String>) { ... }
This is the main function of the kotlin main function is mandatory in every Kotlin program like C and C++. It is the entry point of any Kotlin program.The Kotlin compiler begins executing kotlin program code from the main function.
3. Command Line Arguments (args: Array)
The function takes the array of strings as a parameter and returns Unit which is equivalent to String args[] in java.
4. println
println("Hello, world! - www.littledrops.net")
The println() function prints the given string inside the double quotation with a new line to the screen or output stream. The above line just prints Hello, world! - www.littledrops.net to the screen/stream.