Junior
Can primitive types be used in Kotlin?
sobes.tech AI
Answer from AI
Yes, it is possible. Kotlin supports primitive types, but they are represented as object wrappers.
For example:
Intinstead ofintBooleaninstead ofboolean
When compiling Kotlin code for JVM, these object wrappers can be optimized down to native primitives for performance (e.g., when used in local variables).
fun main() {
val count: Int = 10 // Uses the Int wrapper
val isActive: Boolean = true // Uses the Boolean wrapper
println(count)
println(isActive)
}
Interaction with Java is seamless: Java primitive types are automatically converted to Kotlin wrappers and vice versa.
// Java code
public class JavaClass {
public static int getIntValue() {
return 5;
}
}
// Kotlin code
import JavaClass
fun main() {
val value: Int = JavaClass.getIntValue() // Java int is converted to Kotlin Int
println(value)
}