Table of contents
1.
Introduction
2.
Basic syntax
2.1.
The main() function
2.2.
Variables
2.3.
Functions
3.
Hello World Program
3.1.
Compiling a Kotlin Program
3.2.
Running a Kotlin Program
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

Kotlin Syntax and First Program

Author Vasu Bansal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

This blog will introduce a famous and vital Android development language named Kotlin. It is an open-source language developed by JetBrains.

You will get familiar with the basic syntax of Kotlin and learn fundamental concepts like variables, functions, and many more concepts in Kotlin. Towards the end of the blog, you will be able to write your first Hello World program.

Basic syntax

In this section, we will look at the basic syntax of Kotlin and a lot of related examples to understand its syntax in depth.

The main() function

The main() function is the most important function of a Kotlin application. It acts as the entry point for any Kotlin application. The following code snippet depicts an example of the main function.

fun main() {
    println("Inside main function")
}

Note: As illustrated above, the println() function is used to print messages to the standard output. It also adds a line break after printing the output.

Variables

Variables are abstract storage locations used to store data value inside them. There are two types of keywords used to define variables.

Read-only local variables are defined using the keyword val.

fun main() {
    val x: Int = 10  // immediate assignment
    println("The value of x is $x")
    val y = 4   // `Int` type is inferred
    println("The value of y is $y")
    val z: Int  // Type required when no initializer is provided
    z = 3       // deferred assignment
    println("The value of z is $z")  
}

Output:

The value of x is 10
The value of y is 4
The value of z is 3

Variables that can be reassigned are defined using the var keyword.

fun main() {
    var y = 10 // `Int` type is inferred
    y -= 1 // Reassigning the value of variable named y
    println("The value of y is $y")  
}

Output:

The value of y is 9

Functions

A function is a logical unit of code that performs a specific task. Functions make the code more modular. Functions can be in-built (i.e., imported from some library)  or user-defined. sqrt(), println(), readLine() are examples of standard library functions of Kotlin. Examples of user-defined functions are as follows:
 

A function with two Int parameters and Int return type:

fun sum(x: Int, y: Int): Int {
    return x + y
}

fun main() {
    var res = sum(10,20)
    println("Sum is $res")
}


Output:

Sum is 30


A user-defined function named employee() having different types of parameters-

fun employee(name: String , salary: Int , rating: Char) {
    println("Name of the employee is : $name")
    println("Salary of the employee is: $salary")
    println("Rating of the employee is: $rating")
}

fun main() {
    employee("Ninja",1200000,'A')
}


Output

Name of the employee is : Ninja
Salary of the employee is: 1200000
Rating of the employee is: A

Hello World Program

“Hello, World!”, is the first and the most basic program in any programming language. Let’s write our first program in the Kotlin programming language. This program would be a piece of cake for you if you followed the basic syntax section written above thoroughly. 
 

Open your favourite editor (or open any online IDE) and create a file named helloworld.kt. After that, write the following code in your newly created file and complete your first program in Kotlin. 

// Kotlin Hello World Program
fun main(args: Array<String>) {
    println("Hello World!")
}

Output:

Hello World!

Compiling a Kotlin Program

Any Kotlin program can be compiled using a command-line compiler by giving the following command. 

$ kotlinc <filename.kt>

In this case, we need to type the following command to compile our Hello World program.

$ kotlinc helloworld.kt

Running a Kotlin Program

To run any Kotlin program, use a command-line compiler by giving the following command. 

$ kotlin <filename.kt>

In this case, we need to type the following command to run our Hello World program and see the output on the terminal.

$ kotlin helloworld.kt

 

Note: Semicolons are optional in Kotlin, like other modern programming languages.

Must Read Elvis Operator Kotlin
 

FAQs

  1. Is Kotlin better than Java?
    Kotlin Application Deployment is lightweight, fast to compile, and stops apps from growing in size. Compared to Java, any chunk of code written in Kotlin is substantially smaller, as it is less verbose, and less code implies fewer problems.
     
  2. Can I learn Kotlin without Java?
    You don't need to know Java to learn Kotlin, and you don't need to know Kotlin to learn Java. However, knowing Java will come in handy when interacting with some Java libraries. 
     
  3. Is Kotlin enough for Android development?
    Yes. You can build Android applications using only Kotlin, and as documentation, tooling, and libraries become more Kotlin-focused, building apps exclusively with Kotlin will become even more accessible.

Key Takeaways

Cheers if you reached here!! 

The purpose of this article was to introduce you to the Kotlin programming language, discuss its basic syntax and write your first hello world program in Kotlin.

Yet learning never stops, and there is a lot more to learn. So head over to our Android Development Course on the Coding Ninjas Website to dive deep into Android Development and build the applications of the future. Till then, Happy Learning!!

Live masterclass