Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Learning Kotlin: Object Expressions and SAM Conversions

DZone's Guide to

Learning Kotlin: Object Expressions and SAM Conversions

In this post from "Learning Kotlin," we take a look at object expressions and SAM conversions using the Comparator class in Java.

· Java Zone ·
Free Resource

FlexNet Code Aware, a free scan tool for developers. Scan Java, NuGet, and NPM packages for open source security and open source license compliance issues.

For this post on learning Kotlin, we get to look at something new to an old C# person — object expressions! Object expressions are very similar to anonymous classes in Java where you can declare a class inline rather than entirely separately in its own file.

In the first Koan, we need to implement the Comparator<Int> inline:

fun task10(): List<Int> {
    val arrayList = arrayListOf(1, 5, 2)
    Collections.sort(arrayList, object : Comparator<Int> {
        override fun compare(o1:Int, o2:Int):Int {
            return o2 - o1;
        }
    })
    return arrayList
}


You can see on line three, we define the new object with the object keyword and, then, use : Comparator<Int> to state that it implements that interface. The Comparator has a single function that needs to be implemented, as you can see in line four.

The second Koan takes this further and states that, if there is a single method in an abstract class or interface, we can use the Single Abstract Method (SAM). This is done to avoid needing the class since we just need to implement the single function. To achieve this with the Koan, we use an anonymous function that handles the compare function of the  Comparator :

fun task11(): List<Int> {
    val arrayList = arrayListOf(1, 5, 2)
    Collections.sort(arrayList, { x, y -> y - x})
    return arrayList
}


Lastly, if we look back to the previous post, we can use the extension method to simplify it further:

fun task12(): List<Int> {
    return arrayListOf(1, 5, 2).sortedDescending()
}


These Koans have given me more thoughts about the language than probably any previous Koans:

  1. Why do classes that implement an interface use override? In C#, when you implement an interface, you do not need to state that you are not overriding the functions (see this example). In C#, the only state that you're overriding is when you inherit from a function and you override a function. The reason is that an interface in Kotlin is closer to an abstract class than in C#, to the point it can have functionality. Yep, interfaces can have functions and logic!
  2. So, why does Kotlin have interfaces and abstract classes? The key difference is that an abstract class can have a state, while the logic in an interface needs to be stateless!
  3. Why bother having SAM? As I was working on the Koan, I was delighted by the SAM syntax. But, then, I wondered why I needed this at all? Why is Collections.sort taking a class as the second parameter? Why not just pass in a function, since that is all that is actually needed? Both C# and Kotlin support passing functions so that this is possible. However, something I never knew about Java is that it doesn't support passing functions! You have to use Callable, a class, to pass functions.

 Scan Java, NuGet, and NPM packages for open source security and license compliance issues. 

Topics:
java ,tutorial ,kotlin ,comparator ,object expressions ,sam

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}