Junior — Middle
Can you name features and capabilities of Kotlin that are not present in Java?
sobes.tech AI
Answer from AI
Kotlin offers a number of features absent in Java that make development more convenient and safer:
- Null safety: built-in type system that prevents NullPointerException at compile time.
- Extension functions: ability to add new functions to existing classes without inheritance.
- Coroutines: a convenient way to write asynchronous and non-blocking code.
- Data classes: automatic generation of equals, hashCode, toString, and copy methods for classes intended to hold data.
- When expression: a more powerful and expressive alternative to switch.
- Lambda expressions with receiver (DSL): allow creating convenient DSLs within code.
- Smart casts: automatic type casting after a check.
Example of a data class and an extension function:
data class User(val name: String, val age: Int)
fun String.isEmail(): Boolean = this.contains("@")
val user = User("Alice", 30)
println(user)
println("test@example.com".isEmail())