First Program in Kotlin
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<String>)
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.
Read More Articles
- Declare Variables In Kotlin
- Read Data Input using Scanner in Kotlin
- Double Data type Usage and Type Conversion in Kotlin
- print and println Data Output in Kotlin
- Arithmetic Operators (Mathematical Operators) in Kotlin
- Unary Operators (Sign, Inverts, Increment, and Decrement) in Kotlin
- Equality Operators (==, !=) and Referential equality Operators (===, !==) in Kotlin
- Printing Variables and Values in Kotlin
- Float Data type Usage and Type Conversion in Kotlin
- Long Data type Usage and Type Conversion in Kotlin
- Comparison Operators in Kotlin
- Byte Data type Usage and Type Conversion in Kotlin
- Is Operator (is and !is) in Kotlin
- In Operator (in and !in) in Kotlin
- Char Data type Usage and Type Conversion in Kotlin
- Read Data Input from Command Line in Kotlin
- Assignment Operators and Augmented Assignment Operators in Kotlin
- Indexed Access Operator [, ] in Kotlin
- Read String Data Input in Kotlin
- Short Data type Usage and Type Conversion in Kotlin
- Boolean Data type Usage and Type Conversion in Kotlin
- Logical Operators in Kotlin
- Elvis Operator (?:) in Kotlin
- Repeat and Its Usage in Kotlin
- Safe Call Operator (?.) in Kotlin