Android Articles

Page 17 of 125

How to call a function after a delay in Kotlin?

Soumak De
Soumak De
Updated on 27-Oct-2021 3K+ Views

Kotlin is based on Java, hence we can use Java-based library functions to delay a function call. In this article, we will be using a Java library function to delay the function call using Timer() and schedule().Exampleimport java.util.Timer import kotlin.concurrent.schedule fun main(args: Array) {    // Execution starting point    println("Hello world!!")    // Delay of 5 sec    Timer().schedule(5000){       //calling a function       newMethod()    } } fun newMethod(){ println("Delayed method call!") }OutputOnce executed, the above piece of code will yield the following output −Hello world!! Delayed method call!

Read More

What's the Kotlin equivalent of Java's String[]?

Soumak De
Soumak De
Updated on 27-Oct-2021 359 Views

String is a collection which is implemented using String class. As per the Kotlin documentation, a string can be defined as follows −Class String : Comparable, CharSequenceIn Kotlin, a string is a collection of characters. Strings are immutable in nature which means they are read-only. The length and elements of a string can be modified once declared.In Java, we have an option to create an empty String array by defining it like String[]. In this article, we will see how we can achieve the same using Kotlin library function.Example: Using arrayOf()Kotlin library provides a function to create an array of ...

Read More

How does the reified keyword work in Kotlin?

Soumak De
Soumak De
Updated on 27-Oct-2021 3K+ Views

"reified" is a special type of keyword that helps Kotlin developers to access the information related to a class at runtime. "reified" can only be used with inline functions. When "reified" keyword is used, the compiler copies the function’s bytecode to every section of the code where the function has been called. In this way, the generic type T will be assigned to the type of the value it gets as an argument.ExampleIn this example, we will see how "reified" is helpful to re-use our code and use the same function to perform similar kind of operation regardless of its ...

Read More

What is the Kotlin double-bang (!!) operator?

Soumak De
Soumak De
Updated on 27-Oct-2021 4K+ Views

In Kotlin, "!!" is an operator that is known as the double-bang operator. This operator is also known as "not-null assertion operator". This operator is used to convert any value to a non-NULL type value and it throws an exception if the corresponding value is NULL. In the following example, we will see how to use this double-bang operator.Example 1In this example, we will consider a variable "name" and as a programmer, we want to throw a NULL pointer exception whenever the value of "name" is NULL. Now, execute the following codefun main(args: Array) { var name: ...

Read More

Difference between List and Array types in Kotlin

Soumak De
Soumak De
Updated on 27-Oct-2021 5K+ Views

List and array are two popular collections supported by Kotlin. By definition, both these collections allocate sequential memory location. In this article, we will take an example to demonstrate the difference between these two types of collections.AttributeArrayListImplementationArray is implemented using Array classList or MutableList interfaces are used to implement a List in KotlinMutableArray is mutable, i.e., the values can be changed.List is immutable in nature. In order to create a mutable list, MutableList interface needs to be used.SizeArray is of fixed size. It cannot increase and decrease in size.MutableList do have 'add' and 'remove' functions in order to increase or ...

Read More

What are constants in Kotlin and how to create them?

Soumak De
Soumak De
Updated on 27-Oct-2021 3K+ Views

In every programming language, we need some variable whose value will never change thoroughout the program. In Kotlin too, we have a keyword to create such a variable whose value will remain as constant throughout the program. In order to declare a value as constant, we can use the "const" keyword at the beginning. In this article, we will learn how we can declare a variable as constant in a different way.Example: Top level declarationExampleKotlin const variable can be declared at the top of the programming language and it can be used throughout the file scope.private const val My_TOP_LEVEL_CONST_VAL = ...

Read More

Sort collection by multiple fields in Kotlin

Soumak De
Soumak De
Updated on 27-Oct-2021 1K+ Views

A Collection is an object where developers can group different types of related objects in one place. There are different kinds of collections present in Kotlin library such as List, Array, etc.In this article, we will see how we can sort a collection by different properties present inside that collection. In order to do that, we will be taking the help of the following two functions provided by the Kotlin library.sortedWith()compareBy()sortedWith()sortedWith() is a function provided by the Kotlin library that returns a list of all the elements sorted by a specified comparator. According to official Kotlin documentation, the function definition ...

Read More

What is the difference between "var" and "val" in Kotlin?

Soumak De
Soumak De
Updated on 27-Oct-2021 3K+ Views

In Kotlin, we can declare a variable using two different keywords: one is var and the other one is val. In this article, we will take an example and demonstrate how these declarations are different from each other.AttributevarvalDeclarationvar varName="hello World"val sName = "tutorialspoint.com"ImmutabilityMutableImmutableNo. of times a variable can be assigned Can be assigned multiple times.Cannot be assigned multiple times.Reassigned Can be reassignedCannot be reassignedExampleIn the following example, we will see how we can declare two different variables using "val" and "var". We will also see that the variable declared using 'var' can be changed, while the variable declared using 'val' cannot be ...

Read More

What is the difference between "const" and "val" in Kotlin?

Soumak De
Soumak De
Updated on 27-Oct-2021 7K+ Views

const KeywordThe const keyword is used in Kotlin whenever the variable value remains const throughout the lifecycle of an application. It means that const is applied only on immutable properties of the class. In simple words, use const to declare a read-only property of a class.There are some constraints that are applied to the const variable. They are as follows −const can only be applied to the immutable property of a class.It cannot be assigned to any function or any class constructor. It should be assigned with a primitive data type or String.The const variable will be initialized at compile-time.ExampleIn ...

Read More

Kotlin – Property initialization using "by lazy" vs. "lateinit"

Soumak De
Soumak De
Updated on 27-Oct-2021 11K+ Views

Kotlin library provides two different access modifiers for property declaration.In this article, we will highlight the difference between these two access modifiers and how we can use them in our application.LateinitIn order to create a "lateInit" variable, we just need to add the keyword "lateInit" as an access modifier of that variable. Following are a set of conditions that need to be followed in order to use "lateInit" in Kotlin.Use "lateInit" with a mutable variable. That means, we need to use "var" keyword with "lateInit"."lateInit" is allowed only with non-NULLable data types."lateInit" does not work with primitive data types."lateInit" can ...

Read More
Showing 161–170 of 1,250 articles
« Prev 1 15 16 17 18 19 125 Next »
Advertisements