Does Kotlin have primitive types?
Asked Answered
C

4

32

Does Kotlin have primitive types?. When I declare the variable: val myAge: Int = 18 then the myAge variable stores the actual values is 18 or stores the addresses of the objects in the memory?. If Int is primitive type then why we can use its method like myAge.minus(10)?

Image source: stackoverflow.com

Controvert answered 8/8, 2019 at 8:30 Comment(0)
M
49

No... and yes.

Kotlin doesn't have primitive types (I mean you cannot declare primitive directly). It uses classes like Int, Float as an object wrapper for primitives.

When kotlin code is converted to JVM code, whenever possible, "primitive object" is converted to java primitive. In some cases, this cannot be done. Those cases are, for example, collections of "primitives". For example, List<Int> cannot contain primitive. So, the compiler knows when it can convert an object to a primitive. And, again, it's very similar to Java:

List<Integer> numbers = new ArrayList<>;

numbers.add(0); // <-- you use primitive, but in fact, JVM will convert this primitive to object.
numbers.add(new Integer(0)); // <-- We don't need do that.

Also, when you declare "nullable primitive" it is never converted to primitive (which is kind of obvious, as primitive cannot be null). In Java, it works very similarly:

int k = null; // No way!
Integer kN = null; // That's OK.

One more thing - what docs are saying about it?

For Common, JVM, JS

Represents a 32-bit signed integer. On the JVM, non-nullable values of this type are represented as values of the primitive type int.

For Native

Represents a 32-bit signed integer.

@see: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html

So, the last conclusion. Kotlin doesn't have primitive types out of the box. You treat all objects like objects. Converting to primitive is done at some lower level than code. This design is caused to keep compatibility with JVM.

I did a little deep dive and published it on Medium. For interested: https://medium.com/@przemek.materna/kotlin-is-not-primitive-primitives-in-kotlin-and-java-f35713fda5cd

Maestas answered 8/8, 2019 at 9:2 Comment(3)
Mmm. To put it more directly: Kotlin the language doesn't have primitives; its implementations may use them where possible. On the JVM, that generally means where values are not nullable and not referenced.Bedspring
Related to this topic and might be of interest, check reflectClassUtil.kt:32 eg here: github.com/JetBrains/kotlin/blob/…Florey
Usage of primitives in runtime is directly stated in the Kotlin documentation: "In Kotlin, everything is an object in the sense that you can call member functions and properties on any variable. While certain types have an optimized internal representation as primitive values at runtime (such as numbers, characters, booleans and others), they appear and behave like regular classes to you."Haemal
M
20

Short answer - yes and depends on the declaration.

val myAge: Int = 18           // this is primitive
val myAge2: Int? = 18         // this is not

There's a very informative video about that https://www.youtube.com/watch?v=Ta5wBJsC39s

Memoried answered 8/8, 2019 at 8:35 Comment(4)
If Int is primitive type then why we can use its method like myAge.minus(10)?Controvert
Minus actually is an operator function (public operator fun minus(other: Int): Int) meaning that it's the same as myAge - 10Memoried
Where you would declare this operator overloading method. Obviously inside class so how (Int) could be primitive type.Begone
@Memoried minus is an operator function means that it is implemented inside a class , and that class happens to be the Int class in this example.Folium
C
3

On the Java platform, numbers are physically stored as JVM primitive types, unless we need a nullable number reference (e.g. Int?) or generics are involved. In the latter cases numbers are boxed.

Note that boxing of numbers does not necessarily preserve identity:

val a: Int = 10000
println(a === a) // Prints 'true'
val boxedA: Int? = a
val anotherBoxedA: Int? = a
println(boxedA === anotherBoxedA) // !!!Prints 'false'!!!

Note "===" used to compare reference ....

On the other hand, it preserves equality:

val a: Int = 10000
println(a == a) // Prints 'true'
val boxedA: Int? = a
val anotherBoxedA: Int? = a
println(boxedA == anotherBoxedA) // Prints 'true'
Coffee answered 27/3, 2020 at 15:40 Comment(1)
should be less than 128 - val a: Int = 100 to preserve equality. Please check docsRoehm
A
2

Document say (For Int type):

For Common, JVM, JS: "Represents a 32-bit signed integer. On the JVM, non-nullable values of this type are represented as values of the primitive type int."

For Native: "Represents a 32-bit signed integer."

I tested it (I think these codes run on jvm), and you can see the results in these images. The results were similar for float, double, and other data types.

enter image description here

enter image description here

Alfons answered 8/4, 2023 at 9:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.