Tag

Kotlin

Browsing

In this blog, we’ll learn FOR loop in kotlin Adnroid, will see the exact flow of for loop. How it will work, Will understand the working of FOR loop in detail with the help of an example. So let’s started

FOR LOOP SYNTAX

FOR loop the syntax is for followed by space, bracket open and close. Inside this I simply used the variable of i followed by in operator and defined the range.

for loop syntax
for (initializer in ranges) {
   // put your code here
}
Lets take a simple example
fun main(args: Array<String>) {
    // For loop
    for (i in 1..20) {
        println(i)
    }
}

FOR loop example

Let take a very basic example of FOR loop, Open the IDE and paste below code

fun main(args: Array<String>) {
    for (i in 1..3) {
        println("hi..")
    }
}

Now let us run the code, and see the output on the console

hi..
hi..
hi..
Process finished with exit code 0

So here we simply show that println() method has been executed three times. That is loop 1, loop 2 and loop3  has been executed. After the loop 3, loops actually terminated.

What is the exact flow of FOR loop.

For understanding the exact flow, let take an example, see below code

1 Iteration

In the first iteration( loop 1) the value of i is actually 1 (i=0)and next step comes to the condition check so 1 falls inside the 1 to 3 range. So the condition becomes true then we simply ‘print hi’, and finally at end of the loop simply increment the value of i by 1, So now value of i becomes 2 (i=2).

2 Iteration

Now value i=2 comes the vary initial point of 2 iterations. In the case in second iteration again we have to condition check 2 actually falls inside the range 1 to 3 range, and the condition become true again and again we ‘print hi’, at end of iteration simply increment the value of I and i become 3 (i=3)

for-loop-second-loop

Now, i=3 again come the initial value of loop 3, or you can say starting point of 3 loop. And gain condition becomes true. Because this 3 is actually present inside the 1 to 3 range right. 

So again we’ll print hello. And finally value of i becomes 4 (i=4). Now at the end of loop 3 when the value of i becomes 4, This will try to initiate 4 loop. Which will never happen, Because i=4 simply make the condition as false. The 4 does not lie inside the range. So finally the loop terminates right.

Overview of FOR loop

Finally, the the complete overview looks like below figure.

How for loop work in kotlin

FOR loop example

Let’s take another example for better understanding. Now suppose I ask to write a program using FOR loop print out all the even numbers starting from 1 to 20. For doing that I simply write if else condition. Such as below

fun main(args: Array<String>) {
    // For loop
    for (i in 1..20) {
        if (i % 2 == 0)
            println(i)
    }
}

So let us now run  the code. So here we go 2,4,6,8…,20. So we prints all event number using this for loop example.

2
4
..
..
18
20
Process finished with exit code 0

Conclusion

In this post, we have learned How does FOR loop works in the case of Kotlin. Thank for reading, Have a Good Day       

In this post, we’ll learn about string templates and in other words what is String Interpolation. in Kotlin we have a special type of concept String Interpolation. In this article, we’ll discuss in detail with example.

Let us start with some example, I’m going to define one variable and appends it with title val

        val website = "Android"
        val title = website + "Developer Blog"
        print(title)

Let print the title value, the output is as expected

Output -  AndroidDeveloper Blog

In Kotlin, we have a concept of String Interpolation, so no longer to use the + keyword or the String concatenation. Now, what is the String Interpolation? let change above statement with String Interpolation.

        // define some variables
        val website = "Android"
        val title = "$website Developer Blog"
        print(title)

Here we removed + sign and used $ keyword followed by the name. So Java compiler what it will do, It will simply evaluate name and then print it with other string.

The output will be the same, so let’s run the code here the output we get Android Developer Blog as expected.

So let’s take some more example, create an android project
        // define some variables
        val title = "Android"
        val name = "$title Developer Blog"

        textView.text = "The full statement is $name"
Now I want to add a number of characters in name value.
        // define some variables
        val title = "Android"
        val name = "$title Developer Blog"

        textView.text = "The full name is $name The length of name is ${name.length}"
Let’s take some numeric value example
        val a = 22
        val b = 23
        textView.text = "The sum of $a & $b => ${a + b}"
Output is - The sum of 22 & 23 => 45
Deal with class
   {     // create a object on rect
        val rect = Rect()
        print("The Rect width is ${rect.width} and height is ${rect.height} ")
    }

    class Rect {
        var width = 12
        var height = 14
    }
Conclusion

In this post, we learned string templates and concepts String Interpolation. This is the beauty of Kotlin, In the one statement, you can manage everything. That why we say Kotlin is more powerful then java and it is more expressive and concise.

Why we used Kotlin for Android Development

In this android app tutorial, I’ll explain, RecyclerView Kotlin implementation in a step by step. In this sample application, we’ll use Kotlin programming language along with RxAndroid and Dagger2. So let’s get started.

What is RecyclerView

If you are an android developer you already familiar RecyclerView. When you need to show scrolling list based on large data set, you should use RecyclerView.

RecyclerView is a flexible and upgraded version of ListView. In the RecyclerView several different works together to display your data. Basically, RecyclerView works on ViewHolder design pattern. Each row managed by ViewHolder. So you can say, each view holder is in responsible for displaying a single item with a view.

Technology Used

In this RecyclerView Kotlin tutorial, I’ll create a sample application that fetch the users details from server and will displayed on RecyclerView. In this sample application, We’ll use following technology

  • Androidx Support Lib
  • Retrofit for APIs calling
  • RxJava
  • Glide
1. Create a RecyclerView Sample App

Let’s move to Android Studio, create a new project with androidx and choose Kotlin language from language dropdown.

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.recyclerviewexample"
        minSdkVersion 15
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

def lifeCycleExtensionsVersion = '1.1.1'
def retrofitVersion = '2.3.0'
def daggerVersion = '2.13'
def glideVersion = '4.9.0'
def rxJavaVersion = '2.1.1'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
    implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"
    implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofitVersion"

    implementation "io.reactivex.rxjava2:rxjava:$rxJavaVersion"
    implementation "io.reactivex.rxjava2:rxandroid:$rxJavaVersion"

    implementation "com.google.dagger:dagger:$daggerVersion"
    implementation "com.google.dagger:dagger-android-support:$daggerVersion"
    kapt "com.google.dagger:dagger-compiler:$daggerVersion"
    kapt "com.google.dagger:dagger-android-processor:$daggerVersion"

    implementation "android.arch.lifecycle:extensions:$lifeCycleExtensionsVersion"

    implementation "com.github.bumptech.glide:glide:$glideVersion"


    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0-beta04'
    implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0-alpha02'
}

After adding these dependencies, Sync the project.

2. Open the MainActvitiy layout file and paste below code.

In this layout file, I’m adding the following widget.

  • RecyclerView for showing user list.
  • AppCompatTextView for showing an error message.
  • ProgressBar for showing loader while getting data from a remote server.
  • SwipeRefreshLayout as a root view for refreshing list item on pull to refresh.
 <?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/swipeRefreshLayout"
        tools:context=".view.MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/usersList"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintEnd_toEndOf="parent"
                android:layout_marginEnd="8dp"
                android:layout_marginRight="8dp"
                app:layout_constraintStart_toStartOf="parent"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginBottom="8dp"
                app:layout_constraintBottom_toBottomOf="parent"
                android:layout_marginTop="8dp"
                app:layout_constraintTop_toTopOf="parent"/>

        <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/list_error"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:text="@string/error_text"
                android:gravity="center"
                app:layout_constraintEnd_toEndOf="parent"
                android:layout_marginEnd="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginBottom="8dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                app:layout_constraintTop_toTopOf="parent"/>

        <ProgressBar
                android:id="@+id/loading_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintEnd_toEndOf="parent"
                android:layout_marginEnd="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginBottom="8dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                app:layout_constraintTop_toTopOf="parent"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
2. Create a data model class for below JSON

The JSON payload looks like below. I need to write a data model to parse that JSON.

{
  "page": 1,
  "per_page": 6,
  "total": 12,
  "total_pages": 2,
  "data": [
    {
      "id": 1,
      "email": "george.bluth@reqres.in",
      "first_name": "George",
      "last_name": "Bluth",
      "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg"
    },
    .
    . 
  ]
}
2.1 For parsing above JSON I’m creating below data model class named is Data.kt
package com.recyclerviewexample.model

import com.google.gson.annotations.SerializedName

data class Data(
    @SerializedName("page")
    val page: Int,
    @SerializedName("per_page")
    val perPage: Int,
    @SerializedName("total")
    val total: Int,
    @SerializedName("total_pages")
    val totalPages: Int,
    @SerializedName("data")
    val users: List<User>
)
2.2 Create another data model class named is User.kt
package com.recyclerviewexample.model

import com.google.gson.annotations.SerializedName

data class User(
    @SerializedName("avatar")
    val avatar: String,
    @SerializedName("email")
    val email: String,
    @SerializedName("first_name")
    val firstName: String,
    @SerializedName("id")
    val id: Int,
    @SerializedName("last_name")
    val lastName: String
)
3. Prepare an XML layout for showing list user list item.

For displaying the user on a list item in RecyclerView, creates layout named is item_user.xml. It contains ImageView for use image and TextViews for the display name and email.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="@dimen/layout_height"
        android:layout_margin="4dp">

    <ImageView
            android:id="@+id/imageView"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:padding="@dimen/standard_padding" />

    <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:gravity="center_vertical"
            android:orientation="vertical">

        <TextView
                android:id="@+id/name"
                style="@style/Title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                tools:text="Name" />

        <TextView
                android:id="@+id/email"
                style="@style/Text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                tools:text="Email" />

    </LinearLayout>

</LinearLayout>
4. Create a RecyclerView Adapter that holds that holds user item.

For the above layout creates a RecycleView holder adapter that holds the user list item

package com.recyclerviewexample.view

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.recyclerviewexample.R
import com.recyclerviewexample.model.User
import com.recyclerviewexample.util.loadImage
import kotlinx.android.synthetic.main.item_user.view.*


class UserListAdapter(var users: ArrayList<User>) :
    RecyclerView.Adapter<UserListAdapter.UserViewHolder>() {

    fun updateUsers(newUsers: List<User>) {
        users.clear()
        users.addAll(newUsers)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, p1: Int) = UserViewHolder(
        LayoutInflater.from(parent.context).inflate(R.layout.item_user, parent, false)
    )

    override fun getItemCount() = users.size

    override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
        holder.bind(users[position])
    }

    class UserViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        private val imageView = view.imageView
        private val userName = view.name
        private val userEmail = view.email

        fun bind(country: User) {
            userName.text = country.firstName + " " + country.lastName
            userEmail.text = country.email
            imageView.loadImage(country.avatar)
        }
    }
}
5. Create a Util class for with help us to display user profile picture
package com.recyclerviewexample.util

import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.recyclerviewexample.R

fun ImageView.loadImage(uri: String?) {
    val options = RequestOptions()
        .placeholder(R.drawable.loader)
        .circleCrop()
        .error(R.mipmap.ic_launcher_round)
    Glide.with(this.context)
        .setDefaultRequestOptions(options)
        .load(uri)
        .into(this)
}
6. Step up the Retrofit client for calling APIs

In src folder create a new Retrofit calling interface named is UsersApi

package com.recyclerviewexample.model

import io.reactivex.Single
import retrofit2.http.GET

interface UsersApi {

    @GET("users")
    fun getUsers(): Single<Data>
}
7. Now create Dagger module file name is ApiModule

In this dagger module, we are providing Retrofit client and User Service using @provide annotation

package com.recyclerviewexample.di

import com.recyclerviewexample.model.UsersApi
import com.recyclerviewexample.model.UsersService
import dagger.Module
import dagger.Provides
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory

@Module
class ApiModule {

    private val BASE_URL = "https://reqres.in/api/"

    @Provides
    fun provideUsersApi(): UsersApi {
        return Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()
            .create(UsersApi::class.java)
    }

    @Provides
    fun provideUsersService(): UsersService {
        return UsersService()
    }
}
8. Let prepare to dagger component named is ApiComponent

In this ApiComponent interface, we are injecting User Service and ListView Model that I’ll create later.

package com.recyclerviewexample.di

import com.recyclerviewexample.model.UsersService
import com.recyclerviewexample.viewmodel.ListViewModel
import dagger.Component

@Component(modules = [ApiModule::class])
interface ApiComponent {

    fun inject(service: UsersService)

    fun inject(viewModel: ListViewModel)
}
9. Now I’m creating a service provider class for calling named is UsersService.
package com.recyclerviewexample.model


import com.recyclerviewexample.di.DaggerApiComponent
import io.reactivex.Single
import javax.inject.Inject

class UsersService {

    @Inject
    lateinit var api: UsersApi

    init {
        DaggerApiComponent.create().inject(this)
    }

    fun getUsers(): Single<Data> {
        return api.getUsers()
    }
}
10. Finally, create a subclass of ViewModel name is ListViewModel

Create a class named is ListViewModel, which extends the ViewModel. In this class, we using fetching user list from a remote server and updating ViewModel value.

package com.recyclerviewexample.viewmodel

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.recyclerviewexample.di.DaggerApiComponent
import com.recyclerviewexample.model.Data
import com.recyclerviewexample.model.User
import com.recyclerviewexample.model.UsersService
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.observers.DisposableSingleObserver
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject

class ListViewModel : ViewModel() {

    @Inject
    lateinit var usersService: UsersService

    init {
        DaggerApiComponent.create().inject(this)
    }

    private val disposable = CompositeDisposable()

    val users = MutableLiveData<List<User>>()
    val userLoadError = MutableLiveData<Boolean>()
    val loading = MutableLiveData<Boolean>()

    fun refresh() {
        fetchUsers()
    }

    private fun fetchUsers() {
        loading.value = true
        disposable.add(
            usersService.getUsers()
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(object : DisposableSingleObserver<Data>() {
                    override fun onSuccess(data: Data) {
                        Log.d("error ", "" + data)
                        users.value = data.users
                        userLoadError.value = false
                        loading.value = false
                    }

                    override fun onError(e: Throwable) {
                        userLoadError.value = true
                        loading.value = false
                        Log.d("error ", "" + e.printStackTrace())
                    }

                })
        )
    }

    override fun onCleared() {
        super.onCleared()
        disposable.clear()
    }
}
11. Open the MainActvitiy and paste below code

Basically, In this class, I observing ViewModel and based on changes. I update the adapter list. Apart from this, I refresh value on pull to refresh using SwipeRefreshLayout component.

package com.recyclerviewexample.view

import android.os.Bundle

import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.recyclerviewexample.R
import com.recyclerviewexample.viewmodel.ListViewModel
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

    lateinit var viewModel: ListViewModel
    private val usersAdapter = UserListAdapter(arrayListOf())

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        viewModel = ViewModelProviders.of(this).get(ListViewModel::class.java)
        viewModel.refresh()

        usersList.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = usersAdapter
        }

        swipeRefreshLayout.setOnRefreshListener {
            swipeRefreshLayout.isRefreshing = false
            viewModel.refresh()
        }

        observeViewModel()
    }

    fun observeViewModel() {
        viewModel.users.observe(this, Observer { countries ->
            countries?.let {
                usersList.visibility = View.VISIBLE
                usersAdapter.updateUsers(it)
            }
        })

        viewModel.userLoadError.observe(this, Observer { isError ->
            isError?.let { list_error.visibility = if (it) View.VISIBLE else View.GONE }
        })

        viewModel.loading.observe(this, Observer { isLoading ->
            isLoading?.let {
                loading_view.visibility = if (it) View.VISIBLE else View.GONE
                if (it) {
                    list_error.visibility = View.GONE
                    usersList.visibility = View.GONE
                }
            }
        })
    }
}

Conclusion

That all about RecyclerView in Kotlin, So guys in this android article, we have learned android Kotlin RecyclerView implementation. I hope it’s helpful for you, then help me by sharing this post on your social media profile such as facebook, twitter, and Linkedin.

If you have any queries please put your comment below.

Android RecyclerView Example in Java

In this article, I’ll tell you gives Kotlin Overview. Android discuss all advantages of Kotlin over Java in Android for Android Development. I will tell you also why Kotlin is more powerful than Java.

Kotlin

In Google I/O 2017 the Kotlin has been announced as the official Android language.  In recent years the Kotlin has gained tremendous popularity in terms of developing android applications. Kotlin language has been developed by JetBrains company, that has created the IntelliJ IDEA and if you are an Android developer then you must be knowing that the Android studio has been built on top of the IntelliJ IDEA. So you can understand the importance of the JetBrains company.  It is the creator of IntelliJ IDEA and also the Kotlin language.

Kotlin Highlights

Kotlin language is actually the open source project primarily under the Apache2 license. Which again promotes the developer ecosystem as it is open source and it makes the developer’s community united and contribute to this open source project more freely.

Is it meant to replace Java?

In your teammates always discuss this question in general discussion. You can we say it just because in Google IO 2017 the Kotlin has been announced as an official language of Android so is it going to replace Java in future. In my opinion, the answers are NOT, because the maximum number of Android projects at present has been developed in Java. So it will not be going to replace

Why Kotlin?

Kotlin is more powerful than Java. It more expressive, concise and multiplatform programming language. Kotlin also provides the safety features for your application such as it is immutable in nature and nullability. These two features make your app more healthy and safe. Kotlin ensures more performance of your application.

I have written separate articles for Why use Kotlin for Android Development? I recommend to read it It gives you more clarity on that.

Feature of Kotlin

1. It’s a JVM language

Java is the first JVM language so similar to Java the Kotlin is also the JVM language so you need to install Java JDK that contains the Java Virtual Machine before you start developing any applications. Let’s see below

how kotlin work
2. Expressive, Concise

Kotlin is more expensive and concise in the less amount of code you can get more output compared to Java where you have to write a large number of lines of code to express the same thing so we can say Kotlin is actually more than size than Java in the least number of lines.

Kotlin avoids all boilerplate code, suppose if you want to write a POJO class, Java takes 50 lines of code for this class, in Kotlin you can achieve in fewer lines. See below code snippets and switch tab.

import com.google.gson.annotations.SerializedName
data class Movie (

	@SerializedName("Title") val title : String,
	@SerializedName("Year") val year : String,
	@SerializedName("imdbID") val imdbID : String,
	@SerializedName("Type") val type : String,
	@SerializedName("Poster") val poster : String
)
import com.google.gson.annotations.SerializedName;

public class Movie {

  @SerializedName("imdbID")
  private String id;
  @SerializedName("Poster")
  private String poster;
  @SerializedName("Title")
  private String title;
  @SerializedName("Type")
  private String type;
  @SerializedName("Year")
  private String year;

  public String getImdbID() {
    return id;
  }

  public void setImdbID(String imdbID) {
    id = imdbID;
  }

  public String getPoster() {
    return poster;
  }

  public void setPoster(String mPoster) {
    poster = mPoster;
  }

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    title = title;
  }

  public String getType() {
    return type;
  }

  public void setType(String type) {
    type = type;
  }

  public String getYear() {
    return year;
  }

  public void setYear(String year) {
    year = year;
  }
}
3. Supported high order functions

It’s simply supported high order functions now in case of Java we can only pass variable or reference variable to the functions ride but in case of Kotlin we can simply pass functions within a function that is a path from variable we can also pass function as a parameter

4. Lambda Expression

The Kotlin also supports the lambda expression

  val product = { a: Int, b: Int -> a * b }
  val result = product(9, 3)
  println(result)
5. Strings Templates

Strings Template awesome feature of Kotlin. That allows strings to contain template expressions. String Template expression starts with a dollar($) sign

var message = "Page Number = $pageNo"
String message = "Page Number = " + pageNo;
6. Kotlin and Java are together interoperable

 The most important feature is that Kotlin and Java are together interoperable. Now, what does this interoperable means? It simply means that inside the same application you can have both Java and Kotlin file.

Supposed you are making an application and you can have Kotlin programming code as well along with the Java programming codes. If currently your application is built on top of Java then please don’t worry about it you can add Kotlin codes in the same Java project.

 The question arises if we have both Java and Kotlin files within the same project then how can they interact now this is the beauty of using Kotlin and Java because there interoperable so you can call the Java functions from the Kotlin file and vice-versa that is you can also call the Kotlin file from the Java files.

Java class using in Kotlin class example

    
    public class Movie {

        private String name;

        public Movie(String s)
        {
            name = s;
        }

        public String getName()
        {
            return name;
        }

        public void setName(String name)
        {
            this.name = name;
        }

        public void bookMovie()
        {
            System.out.println("You booked movie ticket -  " + name);
        }
    }

Access Java class in Kotlin

    val movie = Movie("avengers endgame")
    println(movie.name)
    println(customer.bookMovie())

Thank’s for reading this articles, Upcoming articles I’ll also explain each feature and Kotlin fundamentals in details.