Basic Syntax in Kotlin
In this tutorial, we will learn the basic syntax of Kotlin language. We will go through the key components of a simple Kotlin program.
Kotlin Program
fun main() {
println("Hello, World!")
}
Output
Hello, World!
Basic Syntax of a Kotlin Program
fun main()
This line defines the main function, which is the entry point of a Kotlin program. Thefun
keyword is used to declare a function.{
This opening brace marks the beginning of the main function's body.println("Hello, World!")
This line prints the string "Hello, World!" to the standard output (usually the screen). Theprintln
function is a built-in function in Kotlin for outputting a line of text.}
This closing brace marks the end of the main function's body.
Key Points to Remember
- All Kotlin statements must end with a semicolon (
;
), but it is optional and often omitted due to Kotlin's smart semicolon inference. - The main function is the entry point of a Kotlin program.
- Comments can be added using
//
for single-line comments or/* ... */
for multi-line comments. - Code blocks are enclosed in curly braces
{}
. - Kotlin is case-sensitive, meaning that
Main
andmain
would be considered different identifiers.