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
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;
}
}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
| Input | Outcome | Why it matters |
|---|---|---|
No export.dir entry | out | The 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
- Properties.getProperty searches defaults as well as local entries. If the whole chain has no value for the key, it returns null.
- 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.
- 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.