Posts

Showing posts from July, 2023

Cheat Sheet for Kotlin

Image
  I had prepared a list of most common questions asked during Kotlin Android Interviews for Android developer profile. Therefore, if you are preparing for Android Interviews, then this article is a must for you. Question: What are the visibility modifiers in Kotlin? A visibility modifier or access specifier also called as access modifier is a concept that is used to define the scope of something in a programming language. In Kotlin, we have four visibility modifiers: private : visible inside that particular class or file containing the declaration. protected : visible inside that particular class or file and also in the subclass of that particular class where it is declared. internal : visible everywhere in that particular module. public : visible to everyone. Note: By default, the visibility modifier in Kotlin is public . Question: What are the types of constructors in Kotlin? Constructors in Kotlin are of two types: Primary  — These are defined in the class headers. They cannot hold

Coroutines in Kotlin

Image
𝐖𝐡𝐲 𝐚𝐫𝐞 𝐊𝐨𝐭𝐥𝐢𝐧 𝐜𝐨𝐫𝐨𝐮𝐭𝐢𝐧𝐞𝐬 𝐥𝐢𝐠𝐡𝐭𝐰𝐞𝐢𝐠𝐡𝐭 𝐜𝐨𝐦𝐩𝐚𝐫𝐞𝐝 𝐭𝐨 𝐉𝐚𝐯𝐚 𝐭𝐡𝐫𝐞𝐚𝐝𝐬?  What exactly is thread? A thread is a single sequential flow of execution of tasks of a process in an operating system (OS). OS threads are at the core of Java’s concurrency model. Java threads are just a wrapper at OS thread. There is a one-to-one correspondence between Java threads and system kernel threads, and the thread scheduler of the system kernel is responsible for scheduling Java threads. (As shown in the image) When you create a new Java thread, the JVM must work with the underlying OS to create and manage that thread, which is quite expensive because the JVM must communicate with the OS back and forth throughout the thread's lifetime. This switching is an expensive operation, which makes threads expensive, and you can't launch a large number of threads (e.g., 10,000) at once. However, because Kotlin coroutines are entirely managed by the language (a