Strategic Business Plan for 2019

We moved away from medium! Please go to the official blog for the latest information, new content, and engaging activities: Following up on our token holder transparency report, which you can find in…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Java vs Kotlin

To link the events of the view with those of the service, instead of exposing the Observable directly so that the other party can do the subscription, we expose methods that receive functions as parameters; functions that will be executed when an event occurs either in the view or in the service, such as:

A similar implementation in Java would be like this:

The main difference between these implementations ( java vs kotlin ) is the fact that in Java we need to use the Consumer class to pass a function as a parameter (in this case a function that receives an input parameter of Boolean type and does not return anything). If we wanted this function that we passed as a parameter to return something, we would have to change Consumer to Function, and if we didn’t want to have an input parameter, we would have to use Supplier instead. But the thing does not end here, if instead of having one parameter, we would like to have two, we need to use BiFunction or BiConsumer. But if we would like to have three instead of two, Java does not provide us with a solution as we might expect (TriFunction or TriConsumer), or we build it by ourselves or we use Function <T, Function <U, R >> or Function <Function <T, R>, U>. Any of the solutions is definitely less readable and more complicated to define and implement than that provided by Kotlin, where this option is integrated into the language itself.

When we have to instantiate a Fragment, Android makes us do it through a static method in which we will build a Bundle where we write the arguments that we want to pass to the Fragment. This is because this Bundle is stored by the operating system in order to rebuild this Fragment in case it is destroyed.

In Java we would need to do something like this:

While in Kotlin, using the .apply operator, we would do something like this:

While there is no difference in the number of lines between the two snippets, in Kotlin it is not necessary to keep references to the Fragment or the Bundle, and we can also narrow the scope of each part of the code, since we know that within each .apply we are writing code in which this will refer to the object on which we have made the .apply

The results of the experiment are that it is definitely faster to use coroutines than threads, although this is not a comparison as such between Java and Kotlin, we can compare a functionality widely used in Java (Threads) with a functionality implemented in Kotlin (Coroutines)

These are just some of the changes that Kotlin brings us with respect to Java and that make our lives easier when developing applications for the JVM.

Add a comment

Related posts:

The future of retail 2019

Having been a Director and project lead for 15 years in leading global digital agencies and consultancies including AKQA and Deloitte I have recently co-founded a digital product agency, DLVD…

Verification Process and Trust Level for a Single User

The verification of an individual (single user) is the procedure used to ascertain the identity and geographical position of an individual signed up for Helperbit. The verification of an individual…

The Importance of Social Interaction for Seniors

Before diving into the benefits of social interaction for seniors, let’s quickly look at a few detrimental outcomes that result from not being social. Look at these terrible consequences that can…