
What is Kotlin?
Kotlin is a high-level object-oriented programming language developed by JetBrains in 2011. It is used to develop especially Android apps and server-side apps. It runs on JVM(Java Virtual Machine). So if you are new to this language and have basic knowledge of Java, it will be very easy for you to learn Kotlin.
It has a more concise syntax than Java, which can make code easier to read and write.
Its code can call Java code and vice versa, making it easy to integrate with existing Java libraries and frameworks. It supports many functional programming concepts, such as lambdas, higher-order functions, and immutability.
Why do we use Kotlin?
- It supports immutability(ex. declaring a variable with the ‘val’ keyword.)
- It supports avoiding NullPointerException.In Kotlin, all types are non-null by default, meaning a variable cannot be assigned a null value unless it is explicitly specified with the ’?’ Operator. This helps prevent NullPointerExceptions at compile time.
- It supports functional programming(where we can pass a function as a parameter to another function and a function can return another function) and many more.
- It provides Interoperability. As it is a popular choice for developing Android apps because it can be quickly integrated with Java code that already exists. Kotlin code can be used alongside Java code in the same project since it can be built to operate on the Java Virtual Machine (JVM).
- It supports Type inference. It means that type inference is a mechanism used by Kotlin that is intended to work with Java’s type system. This indicates that type inference can frequently be used in Kotlin code to minimize the need for explicit type declarations while yet preserving Java compatibility.
How to set up Kotlin with your IDE?
NOTE: We are using IntelliJ IDEA as an IDE for Kotlin in this tutorial. Let’s get started,
- First, install the JDK(Java Development Kit) in your system if not downloaded in your system.
- Go to the official site of JetBrains or visit this link, https://www.jetbrains.com/idea/download/#section=linux and download the community version based on your OS (operating System). Now you are ready to go ahead: Open your IntelliJ IDEA and create a project,
- Write the name of your project in the name section, select the location for your project, select Kotlin as a language, select the build system as IntelliJ and select the Jdk versions and finally create.


Comments:
Comments increase the readability of code and are also used for documentation purposes. If we add comments in our program it won’t give an error, because the compiler stops the execution of that line where comments are added. It is the same as we use in Java.
There are two types of comments in Kotlin:
- Single-line comments.
- Multi-line comments.
Single-line comments:
To add comments in a single line we use single-line comments. It is denoted by a double forward slash or //. Anything which is written after “//” will be considered as comments and ignored by the compiler.
Example:

Multi-line comments:
When we want to add comments in multiple lines of the statement, then we use multi-line comments. It is denoted by /*…*/.In the same way, if anything is written after “/*..*/” this will be ignored by the compiler and treated as comments.
Example:

Variables
Variables are containers that are used to store the data that are required in a program or code and later we can perform operations on it.
There are two ways to declare a variable in Kotlin:
- By using the ‘var’ keyword
- By using the ‘val’ keyword Let’s understand them one by one,
By using the ‘var’ keyword
var is a keyword used to declare mutable variables, which can be modified later as per the program’s needs. Here, fun is the keyword used to create functions in Kotlin. We are declaring a variable named as name, using the ‘Var’ keyword and initializing it with the value “Java” and later modifying it into “Kotlin”.


By using the ‘val’ keyword
val is a keyword used to declare immutable variables. It can’t be changed once initialized. Here, fun is the keyword used to create functions in Kotlin. We are declaring a variable named as name, using the ‘Val’ keyword and initializing it with the value “Java” and later trying to modify it into “Kotlin” but it is showing an error that we can’t modify the name value.

Data Type
A data type is used to tell a variable what kind of value will be stored inside it. The data types built into Kotlin can be divided into the following categories:
- Number: This includes data types representing numeric values such as Int, Long, Double, Float, and Short.

- Character: This includes the Char data type, which is used to represent individual characters.

- Boolean: This includes the Boolean data type, which is used to represent true/false values. Array: This includes the Array data type, used to store collections of values of the same data type.

- String: This includes the String data type, which is used to represent a character string or groups of characters.

Note: Kotlin provides us to convert the value of one type into another through the following functions: toInt(),
Operators
It is used to perform operations on the data or operand (where an operand is a value on which we perform some operations through an operator.)
In programming, operators are symbols or keywords that represent an action or operation to be performed on one or more operands, which are the values or data on which the operation is performed.
There are different types of operators in Kotlin
e.g. B.Logic operators (&&, ||,!) as well as arithmetic operators (+, -, *, /,%) and comparison operators (==,!=,, >, =, >=), bitwise operators (and, or, xor, shl, shr, ushr) and others. These operators are used to perform various operations on operands such as B. adding or subtracting numbers, comparing values, or manipulating bits.
Kotlin provides different types of operators:
Arithmetic operators: These include the plus (+), minus (-), multiplication (*), division (/), and modulus (%) operators, which are used to perform basic arithmetic operations on numeric data.

Comparison operators: These include the equals to (==), not equals to (!=), more significant than (>), greater than or equals to (>=), less than (<), and less than or equals to (<=) operators, which are used to compare values and return a Boolean result.

Assignment operators: These include the simple assignment operator (=) and compound assignment operators such as +=, -=, *=, /=, and %=, which are used to assign values to variables.

Unary operators: These include the unary plus (+) and unary minus (-) operators, which are used to indicate the sign of a numeric value, as well as the increment (++) and decrement (–) operators, which are used to increment or decrement the value of a variable by 1.

Logical operators: These include the logical AND (&&), logical OR (||), and logical NOT (!) operators, which are used to perform logical operations on Boolean data.

Bitwise operations: These include the bitwise AND (&), bitwise OR (|), bitwise XOR (^), left shift (<<), right shift (>>), and unsigned right shift (>>>) operators, the You are used to performing bitwise operations on integer data.

Loops
When we want to execute the same statements repeatedly, we use loops in our code.
Let’s understand it in a better way. Suppose you have a large dataset of around 50 students, and inside that you have stored the details of each student, and I want to print all the details of each student.
Now, what will be the approach?
What came to mind when I said this? It might be that you are thinking of writing the println statements 50 times. But you are wrong here, this is not the ideal way of doing this kind of task. So for that, we use Loop, and we can print as many details as we want in a few lines of code.
There are three types of loops in Kotlin:
- While loop
- do – while loop
- For loop (Advanced for loop)
While loop
When to use it?
When we want to run a block of code repeatedly until a specific requirement remains true, then we can use loops there.
Syntax:
while(conditions) {
//statements to execute.
}
Example:

The output will be 1,2,3,4,5,6,7,8,9 in the new line.
do-while loop
In this do-while loop, first, the statement inside the do block will get executed, then it checks the condition and executes until it remains true.
Syntax:
do {
} while (conditions)
Example:
fun main(){
var counter = 1 do {
println(counter) counter += 1
} while (counter < 10)
Explanation:
As we know, by creating a function named main (where executions of our program start) using the “fun” keyword and declaring a variable with the “var” keyword name “counter,” you can give it any name.
Inside the do statement, we are printing the counter value. At first, it will print 1, and then the value will increment by one at each iteration. And when the counter reaches 10, the loop will be terminated. Hence, the output will be 1, 2, 3,.. 9.
For loop (or Advanced for loop)
When we want to work on a group of elements or it may be an array and we want to navigate through all the elements then we move to for loop.
Syntax:
for(newVariable_name invariable_where_data_is_stored) {
}
Here, it is an operator which is used to check if a value exists or not.
Example:

Explanation:
- ‘Fun’ is the keyword, used to declare a function or method.
- Main(), the method where executions start in our program.
- Val is the keyword to declare an immutable variable.
- ArrayOf(), is a function to create an array. Here, we are creating an array of lists of courses.
In this loop, the variable “course” is assigned the value of each element in the “list of courses” collection one by one. The loop will iterate over the collection until all elements have been processed.
Note: Kotlin doesn’t support the traditional for loop(It includes initialization, conditions, and incrementations or decrementations).
Conclusion:
Kotlin offers a powerful and efficient programming language for various applications, making development more productive and enjoyable. Its features, ease of integration, and improved safety mechanisms make it a compelling choice for both beginners and experienced developers alike.
By leveraging Kotlin’s capabilities, developers can build robust, efficient, and maintainable applications, empowering them to deliver high-quality software solutions.
References:
Original Source: Title: “Kotlin Tutorial for Beginners”
Author: Rishika Tiwari
Publication Date: July 18, 2023