Monday, July 29, 2019

Kotlin Introduction and Examples | Kotlin Hello Example | Kotlin For Loop | Kotlin While Loop | Kotlin Array | Android Kotlin


Dear Friends, Now in these days ‘Kotlin’ have been the very popular programming language for software development specially for Android Development so let us have a quick look and then in next Blog/Post we will use it in Android Projects.


Question: What is 'Kotlin'?
Answer: 'Kotlin' is a cross-platform statically typed language developed by JetBrains.

Question: What are the variable types in 'Kotlin'?
Answer: Below are the main variable types in 'Kotlin'-
Characters: 'Kotlin' uses ‘Char’ keyword for Characters. 

Booleans: 'Kotlin' uses ‘booleans’ keyword to represent Booleans which have 2 values true or false.

Number: 'Kotlin' have almost same keywords as Java for Number type- Double, Float, Long, Int, Short, Byte.

Strings: In 'Kotlin' Strings are represented by the 'String' keyword. 

Arrays: In 'Kotlin' Arrays are represented by the Array class, that has get and set functions. We will see some examples in later post. 

Question: How to use/write variables in 'Kotlin'?
Answer: Basically, there are two types of use of variables in 'Kotlin' language –
i)Read only or final variable(define with 'val' keyword)-
val num1: Int = 110 // Int with assignment
val num3: Int // without assignment
val name: String = “Manish” // String type with assignment 

ii)Reassigned variables(define with 'var' keyword)-
var num1: Int = 110 // with assignment
num1 = num1 + 90 // we change the value of Reassigned variables
var num3: Int // without assignment
var num2: 220 // with assignment but without type
var name = “Manish

Question: how to write 'Kotlin' functions?
Answer: We write 'Kotlin' function with 'fun' keyword and arguments with ':' keyword.

An Example of 'Kotlin' function to add 2 number-

fun sum(num1: Int, num2: Int): Int {
return num1 + num2
}

Question: How to use comment lines in 'Kotlin'?
Answer: It is the same as Java and other programming languages:

i)Single line or End of line comment 
//This is test comment line

ii)Multiline or Block
/*This is multiline test
Comment for 
Kotlin*/

=======Let us take some examples for better understanding======
1)HelloKotlin
class HelloKotlin
/*Kotlin fun with
Androidhub4you.com*/
fun main()
{
println("Hello from Androidhub4you.com")

}

Output: Hello from Androidhub4you.com


2)IfElseKotlin
class IfElseKotlin
/*Kotlin fun with
Androidhub4you.com*/
val num1: Int = 50
val num2: Int = 60
fun main()
{
        if (num1 > num2)
{
         println("num1 is greater than num2")
}
else
{
println("num1 is smaller than num2")
}

}

Output: num1 is smaller than num2



3)WhileLoopKotlin
class WhileLoopKotlin
/*Kotlin fun with
Androidhub4you.com*/
fun main()
{
var start = 1 //use 'var' as we want to do increment on start-varaiable
val max = 10 //use 'val' as we just want to comparaisen with max-varaible
while (start <= max)
{
println(start)
start++
}
}

Output:
1
2
3
4
5
6
7
8
9
10


4)ForLoopKotlin
class ForLoopKotlin
/*Kotlin fun with
Androidhub4you.com*/
fun main()
{
//increment one by one, it will include 15 as well
println("---Increment by 1 number, including till number---")
for (num1 in 10..15)
{
println(num1)
}
//increment one by one, it will include 15 as well
println("---Increment by 1 number, without till number---")
for (num1 in 15 until 20)
{
println(num1)
}
//increment by 2 number
println("---Increment by 2 number---")
for (num1 in 20 until 30 step 2)
{
println(num1)
}
println("---Decrement by 1 number---")
//for loop for decrement
for (num1 in 40 downTo 30)
println(num1) //we can exclude brackets if we want to write single line only
println("---Decrement by 2 number---")
//for loop for decrement
for (num1 in 50 downTo 40 step 2)
{
println(num1)
}

}

Output:
---Increment by 1 number, including till number---
10
11
12
13
14
15
---Increment by 1 number, without till number---
15
16
17
18
19
---Increment by 2 number---
20
22
24
26
28
---Decrement by 1 number---
40
39
38
37
36
35
34
33
32
31
30
---Decrement by 2 number---
50
48
46
44
42
40


5)ArrayKotlin
class ArrayKotlin
/*Kotlin fun with
Androidhub4you.com*/
fun main()
{
//use listOf keyword to store array elements
val arrayCountry = listOf("Germany", "USA", "UK", "Sweden")
//use for loop to print all elements in array
for(country in arrayCountry)
println(country)
//let us take one more example, how to create array of numbers using toList keyword
val arrayNumbers = (50..60).toList()
for (num in arrayNumbers)
println(num)

}

Output:
Germany
USA
UK
Sweden
50
51
52
53
54
55
56
57
58
59
60

Note: In order to run Kotlin programmes you may use Eclipse with Kotlin-Plugin or use IntelliJ-IDE. Let me know if need more assistance.

Thanks,
Manish

No comments :

Post a Comment