Posts

Showing posts from 2020

Kotlin Some important unsorted interview questions and answers

Image
  What is inline class in Kotlin and when do we need one? Provide an example.  What is inline class in Kotlin and when do we need one? Provide an example. Senior Answer Sometimes it is necessary for business logic to create a wrapper around some type. However, it introduces runtime overhead due to additional heap allocations. Moreover, if the wrapped type is primitive, the performance hit is terrible, because primitive types are usually heavily optimized by the runtime. Inline classes  provide us with a way to wrap a type, thus adding functionality and creating a new type by itself. As opposed to regular (non-inlined) wrappers, they will benefit from improved performance. This happens because the data is inlined into its usages, and object instantiation is skipped in the resulting compiled code. inline class Name ( val s : String ) { val length : Int get ( ) = s . length fun greet ( ) { println ( "Hello, $s " ) } } fun main (