Found 1879 Articles for Mobile Development

What does 'by' keyword do in Kotlin?

Soumak De
Updated on 23-Nov-2021 07:25:10
Kotlin supports delegation of design pattern by introducing a new keyword "by". Using this keyword or delegation methodology, Kotlin allows the derived class to access all the implemented public methods of an interface through a specific object.ExampleIn this example, we will implement an abstract method of a Base class from another class.interface Base {    //abstract method    fun printMe() } class BaseImpl(val x: Int) : Base {    // implementation of the method    override fun printMe() { println(x) } } // delegating the public method on the object b class Derived(b: Base) : Base by b ... Read More

What is "out" keyword in Kotlin?

Soumak De
Updated on 23-Nov-2021 07:23:31
"Out" keyword is extensively used in Kotlin generics. Its signature looks like this −ListWhen a type parameter T of a class C is declared out, then C can safely be a super type of C. That means, a Number type List can contain double, integer type list.ExampleThe following example demonstrates how you can use the "out" keyword in Kotlin −fun main(args: Array) {    var objet1 = genericsExample(10)    var objet2 = genericsExample(10.0) } // As generic type is declared as "out", // we can pass Int and Double also. class genericsExample(input:Any?) {    init {       ... Read More

How to read a text file from resources in Kotlin?

Soumak De
Updated on 23-Nov-2021 07:18:26
In this article, we will see how we can read a text file using Kotlin library functions. Kotlin is based on Java, hence we can use different Java library functions in Kotlin too.Example - BufferedReaderGo ahead and create a Kotlin file in your workspace and name it " ReadFile.kt". Keep a text file with some data in the same directory. For this example, our Text file looks like this −Welcome to the best tutorial website - www.tutorialsPoint.com This is the Text file that we want to read via KotlinExecute the following piece of code to read the above text file.// ... Read More

What does "?:" do in Kotlin? (Elvis Operator)

Soumak De
Updated on 23-Nov-2021 07:16:42
Elvis operator is very common in many programming languages. This is a binary expression that returns the first operand when the expression value is True and it returns the second operand when the expression value is False. Generally, the Elvis operator is denoted using "?:", the syntax looks like −First operand ?: Second operandExampleThe following example demonstrates how you can use the Elvis operator in Kotlin.fun main(args: Array) { val x: String? = null val y: String = x ?: "TutorialsPoint.com" // it will check whether the value of x ... Read More

Difference between a "class" and an "object" in Kotlin

Soumak De
Updated on 23-Nov-2021 07:14:45
Kotlin is a statistically typed language. It has been built over Java, hence it inherits all the object-oriented programming concepts of Java. In this article, we will see the difference between a "class" and an "object" in Kotlin.A "class" is a blueprint of a runtime entity and an "object" is its state, which includes both its behavior and state. In Kotlin, class declaration consists of a "class header" and a "class body" surrounded by curly braces, similar to Java. A general-purpose class definition looks like this −// class Header Class myClass { // class Body }There are ... Read More

When to use an inline function in Kotlin?

Soumak De
Updated on 23-Nov-2021 07:12:39
Kotlin is a statistically typed language. It has different options to handle higher-order functions. Kotlin came up with a wonderful solution for higher-order functions by introducing inline functions.An Inline function is a kind of function that is declared with the keyword "inline" just before the function declaration. Once a function is declared inline, the compiler does not allocate any memory for this function, instead the compiler copies the piece of code virtually at the calling place at runtime.You should opt for an inline function in Kotlin in the following situations −When you need to access higher-order functions.When you need to ... Read More

Difference between "*" and "Any" in Kotlin generics

Soumak De
Updated on 23-Nov-2021 07:09:47
In any programming language, generics are a powerful feature using which developers can create custom data type to manipulate a program in a different manner. There are many ways in which we can define generics in Kotlin.In this article, we will demonstrate the difference between "*" and "Any" keywords in Kotlin.When we define a collection with "*", it should contain the object of only that type. There should not be any mix and match between the data types inside a collection.If we use "Any", we can mix and match the data types, which means we can have multiple data types ... Read More

How to check "instanceof" class in Kotlin?

Soumak De
Updated on 23-Nov-2021 07:04:40
Kotlin is a cross-platform, statistically typed, general-purpose programming language. It is very popular among the developer community because of its interoperable nature with JVM. In the programming world sometimes it is required to check the type of an object to implement a business logic.Unlike Java, we don't have an "instance of" operator in Kotlin. However, we have an "is" operator in Kotlin for type checking and casting.ExampleThe following example demonstrates how the "is" operator works in Kotlin.fun main(args: Array) { val x: String = "TutorialsPoint" // checking the instance and matching the type ... Read More

What is the equivalent of Java static final fields in Kotlin?

Soumak De
Updated on 23-Nov-2021 07:01:54
"static" is a Java keyword that helps developers to define a class member, whereas the keyword "final" is used to declare a constant variable in Java. Once a variable is declared as static in Java, the value of the variable remains unchanged in every instance of the object. Similarly, once a variable is declared as final, its value cannot be altered.In this article, we will see how we can implement the same concept using Kotlin library function.Example - using companion objectA "companion object" is an object which is declared in the same file as a class. Both the class and ... Read More

How to clone or copy a list in Kotlin?

Soumak De
Updated on 23-Nov-2021 06:58:48
A list is a type of collection in which data is stored in a sequential manner. We might come across a situation where the contents of one List have to be copied into another. In this article, we will see how to use Kotlin's built-in methods to clone a list.Example - Using toList()toList() is the basic method which can convert a collection into a list. We can use this method to clone an existing list too.fun main(args: Array) {    val x = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9)    val copiedList=x.toList();    println("Given collection: " +x) ... Read More
Previous 1 ... 4 5 6 7 8 9 10 ... 188 Next
Advertisements