What’s Jetpack Compose and its Benefits? Jetpack Compose is a modern UI toolkit recently launched by Google which is used for building native Android UI. It simplifies and accelerates the UI development with less code, Kotlin APIs, and powerful tools. Declarative Compatible Increase development speed Concise and Idiomatic Kotlin Easy to maintain Written in Kotlin To read more, refer to the article: Jetpack Compose in Android Aaaaaa
Nullable Non Nullable Types What’s Null Safety and Nullable Types in Kotlin? What is the Elvis Operator? Kotlin puts a lot of weight behind null safety which is an approach to prevent the dreaded Null Pointer Exceptions by using nullable types which are like String? , Int? , Float? etc. These act as a wrapper type and can hold null values. A nullable value cannot be added to another nullable or basic type of value. To retrieve the basic types we need to use safe calls that unwrap the Nullable Types. If on unwrapping, the value is null we can choose to ignore or use a default value instead. The Elvis Operator is used to safely unwrap the value from the Nullable. It’s represented as ?: over the nullable type. The value on the right hand side would be used if the nullable type holds a null. var str: String ? = "JournalDev.com" var newStr = str?: "Default Value" str = null newStr = str?: "Default Value" How is !! different from ?. in unwrappi...
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 ma...
Comments
Post a Comment