Found 1952 Articles for Apps/Applications

How can I get a random number in Kotlin?

Soumak De
Updated on 27-Oct-2021 11:10:06

14K+ Views

Kotlin provides multiple ways to generate a random number. In this article, we will see different ways to generate a random number and access it throughout the program.Example – Using Random classRandom() is an abstract class which generates random numbers with the given conditions. It can be accessed after importing Kotlin.random.Random.As per the Kotlin documentation, the companion object Random.Default is the default instance of Random class. In the following example, we will generate a list of random values with int (1-30) .Exampleimport kotlin.random.Random fun main() {    val myRandomValues = List(5) { Random.nextInt(0, 30) }    // Prints ... Read More

How to get the current index of an array while using forEach loop in Kotlin?

Soumak De
Updated on 27-Oct-2021 11:05:59

5K+ Views

Sometimes it becomes necessary to access the index of an array. In this article, we will see how we can access the index of an array in Kotlin while using forEach loop.Example: Using forEachIndexed()nstead of using forEach() loop, you can use the forEachIndexed() loop in Kotlin. forEachIndexed is an inline function which takes an array as an input and its index and values are separately accessible.In the following example, we will traverse through the "Subject" array and we will print the index along with the value.Examplefun main() { var subject = listOf("Java", "Kotlin", "JS", "C") ... Read More

How does the reified keyword work in Kotlin?

Soumak De
Updated on 27-Oct-2021 11:02:33

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
Updated on 27-Oct-2021 10:59:30

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
Updated on 27-Oct-2021 10:56:27

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

Extend data class in Kotlin

Soumak De
Updated on 27-Oct-2021 10:52:51

3K+ Views

Data class is a class that holds the data for an application. It is just like a POJO class that we use in Java in order to hold the data.In Java, for data class, we need to create getter and setter methods in order to access the properties of that class. In Kotlin, when a class is declared as a data class, the compiler automatically creates some supporting methods required to access the member variable of the class. The compiler will create getters and setters for the constructor parameters, hashCode(), equals(), toString(), copy().For a class to be considered as a ... Read More

What are constants in Kotlin and how to create them?

Soumak De
Updated on 27-Oct-2021 10:43:14

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
Updated on 27-Oct-2021 09:16:39

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

How to initialize an array in Kotlin with values?

Soumak De
Updated on 27-Oct-2021 09:00:20

2K+ Views

An array is a type of data structure that contains a definite number of similar types of values or data. In this data structure, every element can be accessed using the array index that usually starts at "0".In Kotlin, arrays can be created using the function arrayOf() or using an Array constructor.Important points regarding Arrays in Kotlin −Arrays are stored in a sequence as per the memory location is concerned.All the elements in an array can be accessed using their index.Arrays are mutable in nature.In conventional programming, the size usually declared along with its initialization, hence we can conclude that ... Read More

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

Soumak De
Updated on 27-Oct-2021 08:56:40

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

Advertisements