Everything You need to know about Kotlin as a Beginner(Basic types, Control flows, Return and Jumps, Functions)

Image for post
Image for post

List of Contents

1. Hello World

fun main(args: Array<String>) {
println("Hello World!")
}

2. Basic Types : Double, Float. Long, Int, Short, Byte

var i: Int = 3;
var d: Double = 3.128;
var f: Float = 3.128F;
var l: Long = 3124_72819_728191; // Here the underscores will not serve and give 312472819728191
var s: Short = 14;
var b: Byte = 2;

println("Integer : $i");
println("Double : $d");
println("Float : $f");
println("Long : $l");
println("Short : $s");
println("Byte : $b")
val i: Int = 3;
// You cannot assign again for i
val aNullableInt: Int?
aNullableInt = 43
// Remember you need to initialize it before use it.
print("aNullableInt : $aNullableInt");
val anInt: Int? = 1673;
val aFloat: Float? = anInt?.toFloat()
print("Int is casting to Float : $aFloat");
val anFloat: Float = "1673.56".toFloat()
print("String is converted into Float : $anFloat")

// Output
Int is casting to Float : 1673.0
String is converted into Float : 1673.56

3. Basic Types : String, Char, Array

//  Characters or char
val c: Char = 'C' // You must declare it in single quote
println("Char variable example: $c")

val dollar: Char = '\u0024' // You must declare it in single quote
print("Char dollar Sign example: $dollar")

// Output
Char variable exapmle: C
Char dollar Sign exapmle: $
//  String
val s = "Kotlin Tutorial" // You must declare it in double quote
println("This $s will help you !!!")


// Output
This Kotlin Tutorial will help you !!!
//  Arrays in Kotlin - String Array
val sArray: Array<String> = arrayOf("William","James","Harper","Mason","Evelyn","Ella")
println("The example of String Array in Kotlin.")
println("Array Size : ${sArray.size}")
print("Elements of Array : ")
for (element in sArray){
print(element)
print(" ")
}

// Output
Array Size : 6
Elements of Array : William James Harper Mason Evelyn Ella
//  Arrays in Kotlin - Int Arrayval iArray: Array<Int> = Array<Int>(4) { a -> 2 * a }
// { i -> 2 * i } is an anonymous function
// Also in this is an another possible way of create an int Array
// val exArray : IntArray = intArrayOf(1,2,3,4);
println("The example of Int Array in Kotlin.")
println("Array Size : ${iArray.size}")
print("Elements of Array : ")
for (element in iArray){
print(element)
print(" ")
}

// Output
Array Size : 4
Elements of Array : 0 2 4 6

4. Control Flow : If, When, For, While

//  If / Else Statements in Kotlin

var a: Double = 1.2
var b: Double = 0.1

if(a >= b){
println("a is greater or equal b")
}else{
println("a is less than b")
}


// Output
a is greater or equal b
//  "when" Statement in Kotlin (Same logic in kotlin)
var c: Char = 'a'
var result: String = when(c){
'a' -> "First character of the alphabet"
'b' -> "Second character of the alphabet"
'c' -> "Third character of the alphabet"
else -> "Other character"
}
print("$c : $result")


// Output
a : First character of the alphabet
//  "when" Statement use as a function
fun checkNumber(input: Int) = when(input){
0 ->{
print("This is zero")
}
1,2 -> {
print("This is one or two")
}
in 3..10 -> print("This is between 3 to 10")
else -> print("Any other")
}

checkNumber(5)

// Output
This is between 3 to 10
//  For Loop
val numArray : IntArray = intArrayOf(1,2,3,4,5,6,7,8)

print("Numbers of Array : ")
for(number in numArray){
print("$number ");
}
println()

println("Numbers of Array with indices : ")
for(index in numArray.indices){
println("Value at index $index is ${numArray[index]}")
}

println("Values of Array and indices as pairs : ")
for((index,value) in numArray.withIndex()){
println("Value at index $index is $value")
}

// Output
Numbers of Array : 1 2 3 4 5 6 7 8
Numbers of Array with indices :
Value at index 0 is 1
Value at index 1 is 2
Value at index 2 is 3
Value at index 3 is 4
Value at index 4 is 5
Value at index 5 is 6
Value at index 6 is 7
Value at index 7 is 8
Values of Array and indices as pairs :
Value at index 0 is 1
Value at index 1 is 2
Value at index 2 is 3
Value at index 3 is 4
Value at index 4 is 5
Value at index 5 is 6
Value at index 6 is 7
Value at index 7 is 8
//  while Loop
var x: Int = 5
while(x > 0){
println("x is $x")
x--
}

// Output
x is 5
x is 4
x is 3
x is 2
x is 1

5. Returns and Jumps : break, continue

//  "Break" use in control flows
var numbers:Array<Int> = arrayOf(1,2,3,4,5,6,7,8)
for(eachInt in numbers){
print("$eachInt ")
if(eachInt == 4){
break
}
}

// Output
1 2 3 4
//  "Continue" use in control flows
var numbers:Array<Int> = arrayOf(1,2,3,4,5,6,7,8)
for(eachInt in numbers){

if(eachInt == 4){
continue
}
print("$eachInt ")
}

// Output
1 2 3 5 6 7 8
//  More with "Break" : break use inside two loops
lable1@ for(i in 1..5) {
lable2@for (j in 1..5) {
if (i == 2 && j == 2) {
break@lable1
}
println("$i and $j")
}
}
// Output
1 and 1
1 and 2
1 and 3
1 and 4
1 and 5
2 and 1

6. Functions

fun addTwoInts(x:Int, y:Int): Int{
return x + y;
}

println("Adding Two Numbers : ${addTwoInts(3, 5)}");

// Output
Add Two Numbers : 8
fun addTwoInts(x:Int, y:Int): Int = x + y
fun areaofRectangle(height:Double, width:Double): Double = height + width

println("Area of the Rectangle is : ${areaofRectangle(3.0, 5.0)}");

// Output
Area of the Rectangle is : 8.0
fun greetingofTheDay(person: String?, period:String) : Unit{
if(person != null) {
println("Good $period $person !!!")
}else{
println("Good $period !!!")
}
}

greetingofTheDay(null, "Morning") // Here you can pass null values using ? after the parameter at the function
greetingofTheDay("Alan", "Morning")

// Output
Good Morning !!!
Good Morning Alan !!!
// Passing unknown number of parameters
fun functionofInt(vararg intParams: Int){
for(eachItem in intParams){
println("each params : $eachItem")
}
}

functionofInt(1,2,3,4,5,6,7,8);

// Output
each params : 1
each params : 2
each params : 3
each params : 4
each params : 5
each params : 6
each params : 7
each params : 8
// Infix Notation
infix fun Int.plus(x: Int):Int{
return this + x
}

print("The addition of 2 and 4 = ${2 plus 4}");

// Output
The addition of 2 and 4 = 6
// Recursive Function
tailrec fun factorial(n: Long = 1):Long{
if(n == 1L){
return 1
}else{
return n * factorial(n-1);
}
}

print("Factorial value of 5 is = ${factorial(5L)}");

// Output
Factorial value of 5 is = 120

Conclusion

Mobile I Web I Data Science I AI — We write rich & meaningful content on development, technology, digital transformation & life lessons.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store