
From Java to Kotlin: How Would You Rewrite This Class?
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 ( codigo and nome ) that are set via the constructor Two private fields with default values ( numero = 0 and texto = "EscolaX" ) A constructor that receives codigo and nome as parameters and assigns them using this 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, class
Continue reading on Dev.to
Opens in a new tab



