Sobes.tech
Junior — Middle

Milliseid spetsiifilisi andmetüüpe Kotlinis tead?

sobes.tech AI

Vastus AI-lt

In Kotlin, there are several specific data types that distinguish it from other languages:

  • Nullable types: in Kotlin, variables by default cannot contain null. To allow null, you must explicitly specify the type with ?, e.g., String?.
  • Any: the base type for all objects, similar to Object in 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 (e.g., throw an exception).
  • Data classes: special classes for storing data with automatically generated methods equals(), hashCode(), toString().
  • Sealed classes: allow restricting inheritance hierarchy, useful for expressing limited sets of types.

Example of a Nullable type:

var name: String? = null
if (name != null) {
    println(name.length)
}