Kotlin
Beginner
1 min read
Kotlin vs Java: Key Differences
Example
// Java-style verbose class (for comparison)
// public class Person {
// private String name; private int age;
// public Person(String name, int age) { ... }
// public String getName() { ... }
// // + equals, hashCode, toString ...
// }
// Kotlin equivalent — one line
data class Person(val name: String, val age: Int)
fun main() {
val alice = Person("Alice", 30)
val bob = alice.copy(name = "Bob", age = 25)
println(alice) // Person(name=Alice, age=30)
println(bob) // Person(name=Bob, age=25)
println(alice == bob) // false — structural equality
// Smart cast
val obj: Any = "Hello"
if (obj is String) {
// obj is automatically cast to String here
println(obj.uppercase())
}
// When expression (enhanced switch)
val score = 85
val grade = when {
score >= 90 -> "A"
score >= 80 -> "B"
score >= 70 -> "C"
else -> "F"
}
println("Grade: $grade")
}
Related Resources
Kotlin Reference
Complete tag & property list
Kotlin How-To Guides
Step-by-step practical guides
Kotlin Exercises
Practice what you've learned
More in Kotlin