Kotlin
2026-08-21 09:00
Kotlin Basics taught the syntax for declaring things.
This unit is about the calling side.
And about a feature with no Java equivalent at all.
joinToString — a collection, a separator, a prefix, a postfix.
Written and rewritten as each feature arrives.
By the end: a one-line call available on every collection.
vararg and the spread operator.Unit 1 committed Kotlin to full interoperability.
This is where that commitment first costs something —
and where the payment turns out to be interesting.
Note the to. It looks like syntax. Hold that thought.
Kotlin has no collection classes of its own.
A Kotlin list passed to a Java method needs no conversion.
Because it is a Java list.
No adapter, no copy, no surprise.
Neither exists on java.util.List or java.util.Set.
Kotlin cannot modify Java’s classes — they ship in the JDK.
Cannot subclass them — listOf returns the real thing.
Will not wrap them — that defeats the point.
Three strings in a row. Which is which?
Checked by the compiler.
Name one, and every later one must be named too.
Named arguments do not work on Java methods.
Java bytecode does not reliably preserve parameter names.
The IDE may show them. The compiler cannot verify them.
Positional arguments → omit only from the end.
Named arguments → omit any subset, in any order.
You would need one overload per combination.
For Java callers who need them: @JvmOverloads generates them.
It belongs to no object.
In Java: a class that exists only as a container, modelling nothing.
No class. No instance. No static.
join.kt → a class named JoinKt, with static methods.
Java sees exactly the utility class it expects.
@file:JvmName("StringFunctions") renames it.
const matters at the boundary: Java sees a field, not a method.
A cleaner Kotlin model.
Whatever Java expects, generated underneath.
this may be omittedA static function whose first parameter is the receiver.
No modification, no patching, no proxy.
Java can call it. It is a static method. Extensions are interoperable by construction.
It cannot see private members. An outsider with pleasant syntax.
Which is what makes extending types you do not own safe.
A feature, not an inconvenience: nobody’s extension can silently change your code.
If a class later adds a matching method, it takes over — silently.
Not a defect. Static functions have never been polymorphic.
Good reason not to write an extension a class ought to have as a member.
Must have a custom getter — there is nowhere to put a field.
Always computed, never held.
Every one an extension function on a Java interface.
The collection API you use every day is a library of extensions.
* is requiredIn Java, “one array” or “these elements” depends on context.
A known source of quiet bugs.
Kotlin makes the intent explicit — and lets you mix spread with fixed elements.
to is not syntaxAn extension function on Any, marked infix, returning a Pair.
So mapOf(1 to "one") is a function call taking varargs of Pair.
Something that looks like language support is a library.
Itself a convention, backed by component1() and component2().
Explained properly in unit 7.
split takes a regular expression. . matches anything.
The signature gives no hint. The failure is silent.
A String argument means a literal delimiter. No accidents.
More readable, easier to debug. A regex is not always the right tool.
Triple-quoted strings take no escape sequences.
And destructured is the feature from two sections ago, doing real work.
Indent the literal to match the code; the indentation does not reach the output.
The same shape twice. With a third field it would be three.
Extract a private method.
Removes the duplication.
Adds a method to the class that nothing else should call.
All these features answer one question:
Where should this code live, so it is easy to call and easy to read?
Java answers “inside a class” every time. Kotlin usually does not.
to is a function, not syntax. Map literals are function calls.Classes, Objects, and Interfaces turns to declarations.
Interfaces with defaults, open/final/abstract, constructors, data classes, delegation, and object.
Where you find out how much of a Java class the compiler will write for you.
Kotlin · Defining and Calling Functions