Kotlin Tutorial | Learn Kotlin Programming Language Part -1

Kotlin Tutorial | Learn Kotlin Programming Language Part -1

14 July 2023 0 By Anshul Pal


Kotlin is a trending and modern programming language and it is widely used for developing android apps. It is very easy to learn and it has very simple syntax just like python. We can’t say that it is similar to any one programming language, because when you learn Kotlin then you see that it is like a combination of c, java and python. Our Kotlin tutorial is dedicated to beginners. We are trying to explain Kotlin in the very easiest and simple way.

Points we covered in this tutorial

  • Introduction of Kotlin
  • Basic syntax of Kotlin
  • Comments in Kotlin
  • Variables in Kotlin

Introduction of Kotlin

Kotlin is a modern programming language which was introduced in 2011 by JetBrains. Kotlin is a free and open source programming language and its source code is available on Github. It can be used to develop server side apps, android apps and much more. Kotlin is officially released in 2016 and it has become very popular because it is compatible with java, which means we can use java code and libraries in Kotlin programs. In 2017, Google announced that Kotlin will be an official language for android development. It is influenced by other popular programming languages like Java, C#, JavaScript etc.

Note :- Kotlin file is saved with .kt extension.

Basic Kotlin Syntax

fun main() {
  println("Welcome to Kotlin Tutorial")
}

In the above example, on the first line, we use a fun keyword which is used to declare a function. A function is a block or group of code which performs a particular task. After that, you see the name of the main function. In every Kotlin program you can see this main function and all your code comes under this main function. Now, on the second line, we use a println function which is used to print the output on the new line. In Kotlin, we don’t need to end code statements with semicolons(;).

Print and Println Function

The only difference between the print and println function is that we use the print function to print the output and println function to print our output on a new line.

Kotlin Comments

Comments are statements which are not a part of the code. We use comments to just define our code statements or the purpose of the code. Comments are ignored by the compiler so that they don’t execute. Like other languages, Kotlin supports single line and multi-line comments. Its comments are very similar to Java, C and C++ programming languages.

Single line Comments

Single line comments used for comment only line. A single line comment in kotlin always starts with two forward slashes(//) and end with end of the line.

Example

// This is a single-line comment in kotlin
println("Hello Single line comment") 

Output

Hello Single line comment

Multi line Comments

Multi line comments are used for comment multiple lines. It starts with /* and ends with */. All lines which comes under this are ignored by Kotlin and it will be not part of you code.

Example

/* The code below will print the words Hello World
to the screen, and it is amazing */
println("Hello Multi line comment")  

Output

Hello Multi line comment

Variables in Kotlin

We all know about the basics of variables. Variables are the containers which store data values. Basically, variables refer to a memory location which is used to store data. In Kotlin we create variables by using val or var and after that we assign values to them with an equal (=) sign. Now you have a question about how val is different from var. So, the major difference is that we use val for declaring constant values or the values which we don’t want to change or modify those values, but by using the var keyword we can change or modify the variable values. Similar to python and other programming languages, we don’t need to define a specified variable type(Int for numbers, String for text and many more). In Kotlin, we simply define a variable without defining its data type because Kotlin is smart enough, it is able to understand the data type of your value without your declaration.

Example

fun main() {
var name: String = "APalgorithm" // String
val birthyear: Int = 2023 // Int
println(name)
println("Founded in - "+ birthyear)
}

Output

APalgorithm
Founded in - 2023

Rules for Kotlin Variables

There are the some rules which are followed by the Kotlin for declaring the variables names in Kotlin programming language :

  • A variable name can contain the letters, digits, underscores, and dollar signs.
  • In Kotlin variable names can also begin with the $ and ( _ ) signs.
  • Variables in Kotlin are case sensitive which means apalgorithm and APALGORITHM are two different variable names.
  • Kotlin Variables names start with also a lowercase character but cannot contains whitespaces.
  • Kotlin variables can not have names like var, val, String, Int because they are reserved keyword in Kotlin.
  • In Kotlin Variables names should always start with a letter.

Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing my blog.

Suggested Readings!