whats the difference between these two ways of ins...
# android
t
whats the difference between these two ways of instantiating a string?
j
stringy2 actually has the type of optional string right, while stringy1 is a String, so it would make working with the
stringy1
easier
👍 1
r
The first variable will infer its type, which will be “String” (aka the non-nullable version) The second has to have it declared, and because the type is String?, will require null checks in order to use
t
ohh okay thats not what i was expected, i was actually asking in regards to when/how they are instantiated. like, are they done at the same time? i know java doesnt have the
init
method as far as im aware so im just curious what it does/when i should be concerned with it
g
They are instantiated in order of appearance
👍 4
j
If you're using Android Studio (which I assume you are) if you go to Tools > Kotlin > Show Kotlin Bytecode and then check the IR checkbox and then Decompile again
👍 1
You can see the java code that is generated 🙂, that might help you to see the difference (if any)
t
oh sick! thank you
😃 3
p
You can also make
stringy2
not nullable and initialize it in the init block
a
Java has
initializers
, which can be static or not. The non static ones are pretty much the same as kotlin's
init
afaik.
👍 1