Learn basic Kotlin programming: A conversion type in Kotlin - AkuCode


Type Conversion
            As with Java, Kotlin also provides a higher variable typing order commonly referred to as generic. Have you ever heard of it? If it is not yet quiet, because in this article we will learn how Kotlin implements Generics and how we as a developer can use the functions provided in the Generic library. Implementation wise, generic is very similar to Java but developers of Kotlin have introduced two new keywords namely "out" and "in" to make Kotlin code easier to read and easy for developers.
            In Kotlin, classes and types are very different concepts. What's the difference? Let's look at the example below how Kotlin implements generic.
            In the above code, we have declared one "integer" and then we set that variable to a number variable. This can be done because "Int" is a subclass of the number class, so Type Conversion occurs automatically at runtime and generates output as "1".
            Let's learn something about generic in Kotlin. It is better to use the Generic Data Type when we feel less confident with what Type of Data we will use for our application. Generally, Kotlin generic is defined with <T> where "T" stands for templates, which can be dynamically specified by the Kotlin compiler. In the following example, we will see how to use the generic data type in the Kotlin programming language.

            In the above code snippet, we have created a class with the Type Data Generics Return, which is represented as <T>. Look at the main method, where we have dynamically defined the value when running by proving the type of value. This is how Generics is interpreted by the compiler in Kotlin. We will get the following output in the browser (https://try.kotlinlang.org) After we run this code on the compiler.
            When we want to determine the type of Generics to Super Type, then we need to use the keyword "out", and when we want to specify the type of Generics to one of the sub-types, then we need to use the keyword "in", in the following example, we Using the keyword "out". We can try using the keyword "in".
            In this article we just learned about conversion type only, please understand carefully so that you can become an Android Developer Expert that can be relied on by the client, thanks to the readers.
Comments