do `get()` vals bloat the size of a class instance...
# getting-started
y
do
get()
vals bloat the size of a class instance?
c
Copy code
val foo: Int
is the exact same output binary than
Copy code
val foo: Int
    get() = field
which are both slightly bigger than
Copy code
private val foo: Int
(because that one doesn't have a getter at all)
Though nowadays I don't think it should matter much
y
re:
field
example above, that's what I expected. thanks. what about something like this?
Copy code
class Foo(val a: Int, val b: Int) {
    val c: Int get() = a + b
}
c
Exact same thing as
Copy code
class Foo(val a: Int, val b: Int) {
    fun getC(): Int = a + b
}
In IntelliJ, you can check yourself: • Create a Kotlin file (only works for
kotlin("jvm")
projects) • Tools → Kotlin → Show Kotlin bytecode • Click on your Kotlin code • Click on 'Decompile' This will show you pseudo-Java code generated by your Kotlin code
y
my (admittedly very elementary) question is, does defining class methods increase the size of each instance of the class, or is that just another entry in a function table.
c
All I can say is, the JVM has had 30 years of engineering into making that cheap, so it probably shouldn't matter either way
k
Methods don't increase the size of each instance. They only increase the size of the
Class
, of which there is only one instance (e.g.
Foo::class
) no matter how many instances of the class you create. So it's negligible.
y
wonderful. thanks @Klitos Kyriacou
also thanks @CLOVIS