When to use lazy initialization and when Eager as even if we do lazy then the variable which we will use will still contain some data associated with the following task. So how do we choose between both? Are there any performance and memory efficient differences between the both?
Please explain any best use case as some threads are supporting eager and some lazy.
In general, the advantages of lazy loading are:
And the disadvantages are:
In other languages another disadvantage is extra code to implement that last bit, but Kotlin's by lazy
makes it ridiculously easy!
Obviously the overall benefit depends on how likely you are to need the value at all, and the impact of a delay at start-up compared to later on.