Basics of Kotlin - Part 1
Tags: android, computerscience, programming
Published: 2021-04-22 10:31:04

In the last article, we try to learn about Gradle and App Folder. If you haven't read the previous article, visit this link and read it
<a href="https://dev.to/csj5483/exploring-the-android-studio-part-1-1d2m">Exploring the Android Studio</a>
Before we start working on Android, let's learn about Kotlin first. In the next few articles, we will try to learn the basics of Kotlin like the loop, conditional statements, function, class.
<h3>Kotlin:</h3>

Kotlin is an open-source, statically typed and general-purpose language that runs on Java Virtual Machine(JVM). It was developed by JetBrains Team, the official designer of the most intelligent Java IDE, named Intellij IDEA. It was officially released in February 2016 and in 2017 Google announced it as the official language for Android Development. It combines the features of Object-Oriented Programming (OOPs) and functional programming into a unique platform.

<h4>Features of Kotlin:</h4>
<b>Concise:</b> Kotlin is the language where lines of code can be reduced up to 40%. This makes Kotlin an ideal choice for software development.</li>

<b>Null Safety:</b> Kotlin aimed to eliminate the NullPointerException (null reference) from the code. Kotlin system can easily distinguish between null references and not null references. For example:

```
var name:String="Aman"
name=null //This is will give an error 
To permit null value we can declare it as:
var name:String?="Aman"
name=null
```
<b>Interoperable:</b> Kotlin is fully compatible with Java Code which means existing Java code can be naturally called from Kotlin, and Kotlin code can be used from Java.
<b>Fast Compilation:</b> It is easy to compile the Kotlin code which results in better performance and fast compilation time.
<b>Smart cast:</b> The Kotlin compiler automatically converts the variable to a particular class reference once it's passed through any conditional operator.
<b>Extension function:</b> Kotlin supports a variety of extension function which means it helps to extend the functionality of classes without touching their code.

<h4>Variables:</h4>
Variables are the name we give to computer memory location. These are used to store and manipulate the data. We can declare a variable by using <b>var</b> and <b>val</b> keyword.
```
var name="aman" //string
val age=10 //integer
```
We can also explicitly specify the datatype of the variable:
```
var name:String="aman" //string
val age:Int=10 //integer
```
<b>var:</b> We can change the value of the variable declared using var keyword. Therefore it is known as a <b>mutable variable</b>. For example:
```
var age:Int=8
age=10
print(age)
```
This will give 10 as output

<b>val:</b> We can not change the value of the variable declared using val keyword. Therefore it is known as an <b>immutable variable</b>. For example:
```
val age:Int=8
age=10
print(age)
This will give an error(Val cannot be reassigned)
```
<h4>Data Types in Kotlin:</h4>
Data types are set of relatable values and describe the operations that can be operated on them. It refers to the type and size of data associated with variables and functions. In Kotlin every data type is considered as an object.
Kotlin has the following categories of data type:
<b>Number:</b> 
This data type holds only number type data variable. It is further sub-categorised as Integers and Floating-Point Numbers.
<ul><li><b>Integers:</b> It contains numeric value without decimal. It has four types:</li></ul>
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/53hjb8q6u44c1y9kq7ct.png)
 
```
var byteVar:Byte=29 //byte data type
var shortVar:Short=323 // short data type
var intVar:Int=40000 //int data type
var longVar:Long=8673463 // long data type
```

<ul><li>
<b>Floating point:</b> It contains non Integer numbers that carry some decimal values. It has two types- <b>Float</b> which has a 32-bit single-precision floating-point value and <b>Double</b> which has a 64-bit single-precision floating-point value.
</li></ul>
```
var floatVar:Float=29.0f // float data type
var doubleVar:Double=8450.445 //double data type
```

<b>Boolean:</b>
It represents the logical value. It contains either true or false.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mfslab0sshmt356evz6g.png)
```
var boolVar:Boolean=true //boolean data type
``` 

<b>Char:</b>
Character in Kotlin is represented with the help of <b>char</b> keyword. Character can be declared using a single quote <b>'c'</b>.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vwbb415oxhnfddgwdk7v.png)
```
var charVar:Char='c' //char data type
``` 
<b>String:</b>
Strings are the arrays of character means a string will contain one or more than one characters. Strings in Kotlin are immutable, which means we cannot change the elements in String. Kotlin has two kinds of string <b>raw string</b> and <b>escaped string</b>. Escaped String allows providing extra line space after the first print statement.
```
var escapedString:String="first name \n" //escapedString
var rawString:String="last name" //rawString
```
<b>Arrays:</b>
Arrays are used to store homogenous data. We can create arrays of different data types like an array to store int value and another array to store string value. Arrays are represented by <b>Array</b> class. We can create array using library function arrayOf() and Array() constructor.
```
Differents approach to declare array
val stringArray=arrayOf("name","age")
val stringArray=arrayOf<String>("name","age")
val stringArray=Array(2){"name";"age"}
val stringArray=Array<String>(2){"name";"age"}
```
We can access elements of an array using the index. For example, if we want to access the first element then we can access it with 0 index.
```
val stringArray=Array<String>(3){"first";"second";"third";}
print(stringArray[1])
This will third as output
```

> Array index starts from 0. So if we are accessing the first element then we have to use the index 0.


That's it for this article. We will continue in the next article.
Happy Learning!!