Let's play a little game. You're a developer who just received a task:
"Take this Java class and rewrite it in Kotlin β keeping the exact same behavior."
Here's the Java class you've been handed:
public class AlunoJava {
private String codigo;
private String nome;
private int numero = 0;
private String texto = "EscolaX";
public AlunoJava(String codigo, String nome) {
this.codigo = codigo;
this.nome = nome;
}
}
Take a moment. How would you write this in Kotlin? π€
Understanding What This Class Does
Before jumping to Kotlin, let's break down what this Java class actually contains:
- Two private fields (
codigoandnome) that are set via the constructor - Two private fields with default values (
numero = 0andtexto = "EscolaX") - A constructor that receives
codigoandnomeas parameters and assigns them usingthis
Simple enough. Now let's talk about how Kotlin handles each of these.
Step 1: The Class Declaration
In Java, you write:
public class AlunoJava { ... }
In Kotlin, classes are public by default, so you don't need the public keyword:
class AlunoKotlin { ... }
β
No public needed β less boilerplate from the start.
Step 2: The Constructor
This is where Kotlin really shines. In Java, the constructor is a separate block:
public AlunoJava(String codigo, String nome) {
this.codigo = codigo;
this.nome = nome;
}
In Kotlin, you can declare the primary constructor directly in the class header. And if you add val or var to the parameters, Kotlin automatically creates the fields and assigns them β no need for this.x = x at all:
class AlunoKotlin(private val nome: String, private val codigo: String) {
// nome and codigo are already declared AND assigned here!
}
β The constructor declaration, field declaration, and assignment all happen in one line.
Step 3: val vs var
You might be wondering: why val instead of var?
-
valβ immutable (likefinalin Java) β value can't be reassigned after initialization -
varβ mutable β value can be changed later
Since the original Java class doesn't reassign codigo or nome after construction, val is the more faithful and idiomatic translation.
Step 4: Fields With Default Values
In Java:
private int numero = 0;
private String texto = "EscolaX";
In Kotlin, these go inside the class body, and the types are inferred automatically:
private var numero = 0 // Kotlin infers Int
private var texto = "EscolaX" // Kotlin infers String
β
We use var here because these fields may be changed later (they weren't final in Java).
The Final Result
Putting it all together:
class AlunoKotlin(private val nome: String, private val codigo: String) {
private var numero = 0
private var texto = "EscolaX"
}
Compare side by side:
Java (17 lines):
public class AlunoJava {
private String codigo;
private String nome;
private int numero = 0;
private String texto = "EscolaX";
public AlunoJava(String codigo, String nome) {
this.codigo = codigo;
this.nome = nome;
}
}
Kotlin (3 lines):
class AlunoKotlin(private val nome: String, private val codigo: String) {
private var numero = 0
private var texto = "EscolaX"
}
Same behavior. A fraction of the code. π―
Key Takeaways
| Java concept | Kotlin equivalent |
|---|---|
public class |
class (public by default) |
| Separate constructor block | Primary constructor in class header |
this.x = x assignment |
val/var in constructor parameters |
private String x |
private val x: String |
Type before name (String x) |
Type after name (x: String) |
final field |
val |
| Mutable field | var |
Conclusion
Kotlin was designed to eliminate Java boilerplate without sacrificing clarity. Once you get used to the primary constructor syntax and the val/var distinction, you'll find that most simple Java classes translate to Kotlin naturally β and end up much shorter.
Have you started using Kotlin in any of your projects? Drop a comment below! π
Top comments (0)