Java To Kotlin Part 4 — Loops, Switches, and Sealed Classes

Michael Duivestein
Tech@Travelstart
Published in
3 min readNov 19, 2018

Part 4 covers basic loop designs, and some interesting types, like ranges. Part 4 also dives into switches and demonstrates just how much more powerful they are when compared to Java’s bland offering. Finally, we’ll look into sealed classes, one of Kotlin’s more advanced features.

Loops

Some data is needed to demonstrate loops. Let’s use this:

Kotlin’s for each method works the same as Java:

Ranges

Should you need a more traditional loop (for instance, if you need to break), you can do this:

This (..) is known as a range in Kotlin. It accepts any number, including Int, Long, Double, Float, Boolean, Char (not exactly a number, but it works), etc. You can even create a custom range if you like. Should you need to count skip numbers (counter+2), the step command can be added:

Ranges and Progressions

Using the ranges example, we can simplify the code with the until function:

downTo is the reverse of until:

“When”

Kotlin’s equivalent of the case statement is really fun to work with. Thanks to the way it was designed, it can make use of a surprisingly large range of values Here’s the basic syntax:

The output is pretty much what’s expected of a case statement:

This can be further refined by extracting println() to outside of the when:

Using when as an expression like this is strongly recommended over a more traditional-appearing switch-like when. This is because compile-time checking will enforce that all cases are covered by the statement.

Let’s make Busiswe and Susan say something different. We’ll do this by combining two cases using a comma:

Using Ranges In “when”

Ranges work the same way in loops as they do in when:

The when can also return data if needed:

Smart Casting With “when”

Smart casting can be really powerful when used in a case statement:

Sealed Classes

Sealed classes are, in essence, enums on steroids. The sealed class type extends the enum class type, making them a sort of hybrid between an enum and a normal class. Instead of declaring enum values, you declare a class, which extends the sealed class:

As can be seen in the above example, the child classes of WorkType do not need the same method signature as the parent (think normal Kotlin inheritance), so long as they call Kotlin’s equivalent of super. In addition, being an enum/class hybrid, sealed classes also keep state like a normal class. This means an instance of a sealed class will maintain it’s data integrity.

We’ll use the following data as an example:

Sealed classes work particularly well with when. Bare in mind that the case statement needs to be complete (or contain an else “default” case), or it could cause problems. A good way to enforce this is to use when as an expression instead of a statement, like in the above examples. Let’s do some work:

As of Kotlin 1.1, child classes of a sealed class do not need to be nested inside the sealed class. They can lie in the same file:

Be aware that this is discouraged. It prevents the sealed class from resembling an enum and it makes it easy to forget certain cases can be when coding.

After expanding the when, everything works just the same:

You’ve reached the end of part 4. Knowledge of loops and enums should have primed you for the hard-to-grasp, but really useful sealed class knowledge.

A good example of when being used in a state machine can be found here.

Examples for Part 4 can be found here.

Part 5 of the series can be found here.

Originally published at www.k0ma.co.za on November 19, 2018.

--

--