Posts

Higher Order Functions and Function Types in Kotlin With Examples

package com.example.my_kotlin_practice fun main (args: Array<String>) { // A higher-order function is a function that takes another function as parameter and/or returns a function. println ( square ( 2 )) var sq:(Int)->Int = ::square println ( "Square of 8 is : ${ sq( 8 ) } " ) var result = calculate4 ( 4 , 5 ,::sum1) println ( "Result is : $ result " ) var a = 10 var b = 20 arithmeticOperation (a,b,::add) arithmeticOperation (a,b,::sub) arithmeticOperation (a,b,::mul) arithmeticOperation (a,b,::div) } fun square (num: Int): Int { return num*num } fun sum1 (a:Int,b: Int) = a + b fun calculate4 (a:Int,b:Int, operation:(Int,Int) -> Int):Int { return operation(a,b) } fun add (x:Int,y: Int): Int { return x + y } fun sub (x:Int,y: Int): Int { return x - y } fun mul (x:Int,y: Int): Int { return x * y } fun div (x:Int,y: Int): Int { return x/y } fun arithmeticOperation (a:Int,b:Int,fn:(I...

Collections (List, Set & Map) in Kotlin

  package com.example.my_kotlin_practice fun main (args: Array<String>){ var list = listOf ( 1 , 2 , 3 , 4 , 5 ) println (list) println (list.contains( 6 )) println (list.contains( 5 )) println (list.get( 0 )) println (list[ 0 ]) var list1 = mutableListOf ( 1 , 2 , 3 , 4 , 5 ) list1[ 1 ] = 11 println ( "'11' is added at '1st' position : $ list1 " ) list1.set( 2 , 10 ) println ( "'10' is added at 2nd position in 'list1' : $ list1 " ) list1.addAll(list) println (list1) val subjectSet = setOf ( "CS" , "Physics" , "Chemistry" , "Maths" , "CS" , "Physics" ) println (subjectSet) println (subjectSet. elementAt ( 3 )) println (subjectSet.contains( "English" )) val subjectSet1 = mutableSetOf ( "CS" , "Physics" , "Chemistry" , "Maths" , "CS" , "...

What is ListView ? | Type of Listview | Listview in Android Studio

Image
  <? xml version ="1.0" encoding ="utf-8" ?> <androidx.constraintlayout.widget.ConstraintLayout xmlns: android ="http://schemas.android.com/apk/res/android" xmlns: app ="http://schemas.android.com/apk/res-auto" xmlns: tools ="http://schemas.android.com/tools" android :id ="@+id/main" android :layout_width ="match_parent" android :layout_height ="match_parent" tools :context =".ListViewActivity" > <ListView android :id ="@+id/listView" android :layout_width ="match_parent" android :layout_height ="wrap_content" app :layout_constraintEnd_toEndOf ="parent" app :layout_constraintStart_toStartOf ="parent" app :layout_constraintTop_toTopOf ="parent" > </ListView> </androidx.constraintlayout.widget.ConstraintLayout> Above is activity_lis...

What is AdapterView in Android? | AdapterView Android Studio Kotlin | AdapterView

Image
 

Exception Handling (Try, Catch, Finally) in Kotlin

  package com.example.my_kotlin_practice fun main (args: Array<String>) { try { var a = "abc" . toInt () } catch (e: NumberFormatException) { println ( "String can't be converted to number because of ${ e. message } " ) } // catch(e: Exception) { // println("String can't be converted to number because of ${e.message}") // } finally { println ( "This will execute everytime" ) } println ( "Processing done" ) // isEven(3) } //fun isEven(num: Int) { // if(num%2 != 0) { // throw IllegalArgumentException("Number is not even") // } //} OUTPUT : String can't be converted to number because of For input string: "abc" This will execute everytime Processing done Process finished with exit code 0

Null Safety in Kotlin

  package com.example.my_kotlin_practice fun main (args: Array<String>){ var s:String = "Android" var str:String? = null println (s. reversed ()) if (str != null ) { println ( str . reversed ()) } println (str?. reversed ()) var len = str?. length ?: 0 println ( "Length is : $ len " ) // println(str!!.length) str?. let { println ( it . length ) } } OUTPUT : diordnA null Length is : 0 Process finished with exit code 0

Extension Function in Kotlin

  package com.example.my_kotlin_practice fun main (args: Array<String>) { var circle = Circle1() println ( "Area of circle is : ${ circle.area( 4 ) } " ) println ( "Perimeter of circle is : ${ circle. perimeter ( 4 ) } " ) var str = "Madam" println ( "Is given ' $ str ' string palindrome ? : ${ str. isPalindrome () } " ) var str1 = "Nurses run" println ( "Is given ' $ str1 ' string palindrome ? : ${ str1. isPalindrome () } " ) } class Circle1{ fun area (r: Int): Double{ return 3.14 *r*r } } fun Circle1. perimeter (r: Int): Double{ return 2 * 3.14 *r } fun String. isPalindrome (): Boolean{ return this . lowercase () == this . lowercase (). reversed () } OUTPUT : Area of circle is : 50.24 Perimeter of circle is : 25.12 Is given 'Madam' string palindrome ? : true Is given 'Nurses run' string palindrome ? : false Process finished with exit c...