Junior — Middle
What are the differences in the type system between Java and Kotlin?
sobes.tech AI
Answer from AI
Java is a statically typed language where primitive types (int, boolean, etc.) and reference types are clearly separated. In Kotlin, types are more unified: all are objects, including primitive analogs, thanks to a wrapper system that is optimized by the compiler.
Main differences:
- Null safety: In Kotlin, types by default cannot contain null to avoid NullPointerException. For nullable types, the syntax with a question mark is used, for example,
String?. - Type parameters: Kotlin supports a more flexible generics system with variance (in, out), which in Java is implemented through wildcards.
- Data types: Kotlin has extended types, for example,
Unitinstead ofvoid, and supports type extensions.
Example of null safety in Kotlin:
var a: String = "Hello"
// a = null // Compilation error
var b: String? = "Hello"
b = null // Allowed