Junior — Middle
What specific data types in Kotlin are you familiar with?
sobes.tech AI
Answer from AI
Kotlin has several specific data types that distinguish it from other languages:
- Nullable types: in Kotlin, variables by default cannot contain
null. To allownull, you need to explicitly specify the type with?, for example,String?. - Any: the base type for all objects, similar to
Objectin Java. - Unit: a type that indicates the absence of a return value, similar to
void. - Nothing: a type that has no values, used for functions that never return a result (for example, throw an exception).
- Data classes: special classes for storing data with automatically generated methods
equals(),hashCode(),toString(). - Sealed classes: allow restricting the inheritance hierarchy, useful for expressing limited sets of types.
Example of a Nullable type:
var name: String? = null
if (name != null) {
println(name.length)
}