Basics of Kotlin - Part 2
Tags: android, programming, computerscience
Published: 2021-04-23 18:31:28

In the last article, we learnt about some basic concepts of Kotlin like what is Kotlin, features of Kotlin, how to declare variables and data types in Kotlin. Before reading this article make sure you had read the last article <a href="https://dev.to/csj5483/basics-of-kotlin-part-1-jh">Basics of Kotlin- Part 1</a>. Let's learn more about Kotlin.

<h3>Kotlin Type Conversion:</h3>
Type Conversion is a procedure of converting one type of data variable into another data type. It is also known as Type Casting. Unlike Java, Kotlin does not support the implicit conversion of a smaller data type into a larger data type. It means we can not assign Int into Long or Double.

<b>In Java:</b>
```
int a=10;
long b=a; //no error
```
<b>In Kotlin:</b>
```
var a=10;
var b:Long=a //compiler error Type mismatch
```
However, we can do the explicit conversion with the help of predefined helper functions. The list of helper functions used for numeric conversion in Kotlin is given below:
<ul>
<li>toInt()</li>
<li>toByte()</li>
<li>toShort()</li>
<li>toChar()</li>
<li>toLong()</li>
<li>toFloat()</li>
<li>toDouble()</li>
</ul>
```
For example:
var a=10;
var b:Long=a.toLong() //compiles successfully no error
```
<h3>Operators:</h3>
Operators are the special characters or symbols which performs the operation on operand(value or variable). There is various kind of operators in Kotlin:

<h4>Arithmetic Operators:</h4>
Arithmetic operators are used to perform basic mathematical operations such as addition (+), subtraction (-), multiplication (*), division (/) and modulo(%)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ljb7lj8rmqfo87w69nhl.png)
```
Example:
var a=11
var b=5
print(a+b) // Output : 16
print(a-b) // Output : 6
print(a*b) // Output : 55
print(a/b) // Output : 2
print(a%b) // Output : 1
``` 

<h4>Relation Operators:</h4>
Relation Operator is used to compare two values, two variables or two numbers. It always gives output as true or false.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sagaqo96zxzxq7lhhxkc.png)
```
For example:
 var a=11
 var b=5
 print(a<b) //Output : false
 print(a>b) //Output : true
 print(a<=b) //Output : false
 print(a>=b) //Output : true
 print(a==b) //Output : false
 print(a!=b) //Output : true
```
<h4>Assignment Operators:</h4>
Assignment Operators are used to assigning the arithmetic operated values. The assignment of value takes from right to left.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2rf30o41852qjqxlizyj.png)
```
For example:
var x = 10
var y = 2
var x + = y // Output : 12
Var x - = y // Output : 8
Var x * = y // Output : 20
```
<h4>Unary Operators:</h4>
Unary operators are used with only a single operand. 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8kjmynun11bzt3tdwjs.png)
> In pre increment/decrement the value will be updated before assigning to the variable and in post increment/decrement the value will be updated after assigning to the variable 
```
var a=10
print(a++) //Output : 10
print(a) //Output : 11
print(++a) //Output : 12
```
<h4>Logical Operators:</h4>
Logical Operators are used to check conditions between operands.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u4xszlthz9dlg9y2lwzl.png)
```
For example
var a=10
var b=5
var c=16
print((a>b)&&(a>c)) //Outout : false
print((a>b)||(a>c)) //Output : true
var flag=true
print(!flag) //Output : false
```
<h4>Input in Kotlin:</h4>
Kotlin uses the standard library function <b>readLine()</b> which read the line of string input from the standard input stream.
```
var name=readLine() //For string input
```
If we want the input of other data types, then we can use Scanner object. For that, we need to import Scanner class from Java standard library using:
```
import java.util.Scanner
```
Then, we need to create Scannerobject from this class.
```
val scannerObj= Scanner(System.`in`)
```
Now we can use this scannerObj for taking the input from the user
```
var name=scannerObj.next() // For String input
var age = scannerObj.nextInt() // For Integer input
var charInput=scannerObj.next().single() //For char input
var doubleInput=scannerObj.nextDouble() //For double input
```
<h4>Comments in Kotlin:</h4>
Comments are used for making the source code easier for humans to understand. They provide explanatory information about the source code. Comments are ignored by the compiler so they will not execute. Kotlin has two types of comments:
<ul>
<li>Single Line: Single line comment is used for commenting single line of the statement.</li>
<li>Multi-line: Multi-line comment is used for commenting multiple lines of the statement.</li>
</ul>
```
//This is single line comment
/* This is
   multiline
   comment */
```
That's it for this article. We will continue in the next article.
Happy Learning!