Angelina
Angelina

Reputation: 1563

Kotlin object creation and memory management compared to Java

I've heard that in Kotlin creation of the new objects is cheap. How is Kotlin memory aspect of object creation is different from Java? Is there difference in cost of creating objects from the data class and class?

Upvotes: 1

Views: 3450

Answers (2)

GreenhouseVeg
GreenhouseVeg

Reputation: 647

I would expect no difference if Java and Kotlin are both compiled for same target VM - it should make no difference which source code produces the same bytecode.

As for data class, Hiosdra correctly pointed out that this is just a syntax sugar to tell the compiler to derive some standard methods useful for data-holding classes (see the documentation).

Upvotes: 1

Hiosdra
Hiosdra

Reputation: 472

I think you mean Kotlin targeting JVM, so I will tell you about this target.

Kotlin uses the same bytecode as Java, so performance in general is the same (some operations can be less or more optimized in Kotlin(thanks to compiler or stdlib) in compare to Java).

Data classes are just normal classes with additionally generated toString(), equals(), hashCode() and clone() methods, so they have the same performance as normal classes.

Upvotes: 3

Related Questions