Skip to workspace
Kotlin Lab

Kotlin on the JVM / Chapter 01

Read a Java platform type

Give a legacy setting three meanings: absent, disabled, or configured.

The decision

An exporter reads export.dir from java.util.Properties. A missing key uses the default folder "out". An explicitly empty value disables export. Any other value is the exact directory name, including its spaces.

Read the Kotlin

Resolve the Java lookup before interpreting its value

Properties.getProperty first looks for a local string value. If the key is missing it consults the defaults chain. An explicit empty string is already a value and stops the search. Follow that lookup to its end before applying the Kotlin rule for a disabled export.

val defaults = Properties().apply { setProperty("dir", "shared") }
val local = Properties(defaults)
local.getProperty("dir")   // "shared"
local.setProperty("dir", "")
local.getProperty("dir")   // ""

Choose a Kotlin type at the Java boundary

An unannotated Java String return can appear as the platform type String! in the IDE. The exclamation mark is a diagnostic notation. In source, you choose String or String?. Assigning a missing property to a non-null String can fail at that assignment, before a later empty-string test. A nullable local lets you make the missing case explicit.

val region: String? = local.getProperty("region")
if (region == null) return "default"

Keep an empty setting distinct from an absent one

Elvis chooses its right-hand result whenever the left side is null. If an earlier step has already converted empty text into null, the fallback cannot recover which kind of absence it saw. Trace a missing setting and an explicitly empty one through the same expression. Use separate branches if disabling the exporter and choosing its default directory are different actions.

The full example

Java 25
import java.util.Properties;

class Lesson {
    static String exportFolder(Properties settings) {
        var folder = settings.getProperty("export.dir");
        if (folder == null) return "out";
        return folder.isEmpty() ? null : folder;
    }
}
Kotlin/JVM
import java.util.Properties

fun exportFolder(settings: Properties): String? {
    val folder: String? = settings.getProperty("export.dir")
    return when (folder) {
        null -> "out"
        "" -> null
        else -> folder
    }
}

Inputs and outcomes

InputOutcomeWhy it matters
No export.dir entryoutThe Java lookup returns null.
export.dir = ""null (export disabled)An empty string is present, so the missing-key fallback must not run.
export.dir = " sales "" sales "Spaces belong to this folder name.

Walk through the code

  1. Properties.getProperty searches defaults as well as local entries. If the whole chain has no value for the key, it returns null.
  2. A Java return type without recognized nullability metadata can appear in Kotlin as String!. Assigning it to String? makes the documented missing-key case part of the Kotlin contract. The when expression then distinguishes missing, explicitly empty, and configured values.
  3. An exporter receiving null from exportFolder can skip work entirely. A missing entry instead returns the usable directory "out". This is why treating every empty-looking value as absent would change the product.

The Java habit

Writing val folder: String = settings.getProperty("export.dir") asserts a stronger contract than the Java method offers. A missing key can fail on that assignment before any fallback runs.

When to choose differently

For a configuration format where absence and an empty value both disable export, a three-way result adds a distinction that callers cannot use. Document the simpler contract at the boundary.

References

Open quizTry code