A type classifies the values an expression or variable can represent and the operations permitted on those values. In Kotlin, types make declarations explicit when inference is insufficient and help the compiler reject invalid assignments and calls. section 7.3 section 11.3.2
Declare, infer, and assign
val question: String = "The Ultimate Question of Life, the Universe, and Everything" val answer = 42 val yearsToCompute = 7.5e6 Here, String is written explicitly, while Kotlin infers Int from 42 and Double from the floating-point initializer. If initialization is delayed, declare the type first: val result: Int result = 42 section 2.1.4
Keep these distinctions in view:
- A class name can directly name a type, but a nullable form such as String? is a different type. section 11.3.2
- Generic classes produce types through type arguments: List<Int>, List<String?>, and List<List<String>> are distinct types. section 11.3.2
- A subtype can be used wherever its supertype is expected: Int fits Number, but Int does not fit String. section 11.3.2
- A non-null type such as Int is a subtype of Int?, but a nullable Int? cannot be used where Int is required. section 11.3.2
Figure 11.5 B is a subtype of A if you can use it when A is expected. Since you can use an Int where a Number is expected, it is a subtype. Likewise, you can use an Int where an Int is expected; it is also a subtype of itself. Because you can’t use an Int where a String is expected, it can’t be considered a subtype.
Use the subtype diagram to check whether one value type can stand in for another. section 11.3.2
















