Part 12 / Topic 4 of 7
Nullability annotations at the boundary
@Nullable/@NotNull (this course uses the JetBrains org.jetbrains.annotations package,
bundled directly with kotlinc — no extra download needed) are what turn a platform type into
a real one. JSpecify's @Nullable/@NonNull are a newer, vendor-neutral alternative that
kotlinc also understands; either works, and a growing number of modern Java libraries ship
one or the other on their public APIs specifically so Kotlin callers get real types instead of
platform types. examples/jspecify-nullability/ is the same @Nullable/non-null pattern as
LegacyCarrierRates.java, driven by JSpecify's annotations instead of JetBrains' — verified to
behave identically on the Kotlin side (a nullable reading forces a check, a non-null one
doesn't), just with jspecify-1.0.0.jar on the classpath instead of the JetBrains jar kotlinc
already bundles.
| Java declaration | Kotlin sees it as |
|---|---|
@Nullable public Double ratePerKgFor(...) |
Double? — a check is required |
@NotNull public String carrierName() |
String — never null, no check needed |
public String operatorNote(...) (no annotation) |
String! — a platform type, section 3's danger |
Done here? The practice set for Part 12 — including the solutions — is on the part page.