CodingKotlin

Kotlin : Basic syntax with Examples

Basic Syntax of Kotlin with Example listed below

Val and Var

val : This is used to define constant field and final variable. if we define variable with final then we can assign it value again. this is immutable type of variable

var : This is general type of variable. we can change/ assign a new value to it.those are mutable variable.

String

You can define string as

var value : String = ""

//use val for final variable

or

val value : String? = null 

//use val for final variable

In first case you can simply assign not null empty string to variable. and second we can assign null value to string by denoting “?” to string. its simply define that string is nullble

val value : String = null //this will show compile time error. as null value can not be assigned to not null type

 

Boolean

Same as String

For not null type

var bBoolean : Boolean = false

and for nullable

var bBoolean : Boolean? = null

lateinit

lateinit define that we can initialize variable later so it want throw compile time error.

var mArrayist : ArrayList // it will suggest that please initialize the variable

You can modified to this

lateinit var mArrayist : ArrayList; yehh... now you can initialize this later on

Basically in Kotlin there is no need to add “new” in Syntax for initialize

just do

var inArrayist : ArrayList = ArrayList();

Object replaced with Any

If you want to define “Object” varible like in Java, you can ues “Any” instead. In kotlin “Any” is the super class of the all the classes

How to define Function in Kotlin

Function without return type : Unit (Same as “void” in java)

fun printValue(x: Int, y: Int): Unit {
print("mulitple of $x with $y is ${x*y}")
}

or

fun printValue(x: Int, y: Int){
print("mulitple of $x with $y is ${x*y}")
}

Function with return type (Two argument)-

fun Multiplier(x: Int, y: Int): Int {
return x * y;
}

Inline function

fun Multiple(x:Int,y:Int) = x*y

calling functions

val muliple = Multiplier(5,10);
var multipleInline = Multiple(10,50)

Use Of Conditional Expressions

If Else Expression

fun isEven(a: Int): Boolean {
if ((a % 2) == 0)
return true
else
return false
}

or

inline

fun isEven(a: Int) = ((a % 2) == 0)

How to Check Type Of Class

Like “Instance of” in Java, We can use “is” operator in Kotlin

fun getListLength(mAny: Any): Int? {
if (mAny is ArrayList<*>) {
return mAny.size
}
return null
}

Inline

fun isInt(x: Any) = x is Int

and for reverse check

fun getListLength(mAny: Any): Int? {
if (mAny !is ArrayList<*>) {
return null
}
return mAny.size
}

for loop

fun seeForLoop(mAnys: ArrayList) {
for (item in alist) {
print(item)
}
for (index in alist.indices) {
print("Index || "+alist.get(index))
}
}

While loop

fun seeWhileLoop(listStrings: ArrayList) {
var indexOflist = 0
while (indexOflist < listStrings.size) {
print(listStrings.get(indexOflist))
indexOflist++
}
}

When : This is alternative of “switch” in Java

fun whenLoop(mAny: Any) {
when(mAny){
is Text -> "This is text"
is Int -> "This is integer"
is Boolean -> "This is boolean"
}
}

Leave a Reply

avatar
  Subscribe  
Notify of