Found 1879 Articles for Mobile Development

What does "return@" mean in Kotlin?

Soumak De
Updated on 16-Mar-2022 13:38:40
return@ is a statement in Kotlin which helps the developers to return a function to the called function. In simple words, return@ can return any value, anonymous function, simple inline function, or a lambda function.Example – return in Kotlinfun main(args: Array) {    val s = message()    println("Message = $s") } fun message():String{    return "Hello Kotlin! This is a returned message." }OutputIt will produce the following output −Message = Hello Kotlin! This is a returned message.Example – Kotlin Labeled returnNow "return@" helps to control the flow to a specific level inside the code. In Kotlin terminology, it is ... Read More

How to initialize an empty array list in Kotlin?

Soumak De
Updated on 16-Mar-2022 13:32:42
Kotlin ArrayList class can be used in order to create an empty arrayList. It would be a dynamic array which means it will automatically expand as we add data into it. An ArrayList is an ordered sequence of elements, however, unlike simple arrays, an ArrayList can contain data of multiple data types.The function definition of arrayList goes like this −fun arrayListOf(): ArrayListIt returns an empty new ArrayList. If a number is provided as the argument, then it will return an arrayList with the given elements.Example: Initialize an empty array in KotlinThe following example demonstrates how you can create a ... Read More

How to convert an ArrayList to String in Kotlin?

Soumak De
Updated on 16-Mar-2022 13:29:18
In this article, we will see how we can convert an ArrayList in Kotlin to a String. In order to do so, we will use a String function called joinToString() that is provided by the Kotlin library. Its definition goes like this −fun Array.joinToString(    // the String will be separated by comma    separator: CharSequence = ", ",        // prefix to the String    prefix: CharSequence = "",        // postfix to the String    postfix: CharSequence = "",        // This number of elements will be printed;    // the ... Read More

How to check generic type in Kotlin?

Soumak De
Updated on 16-Mar-2022 13:16:35
In this article, we will see how we can get the type of a class that is used in Kotlin. There are no direct ways to do this in Kotlin. In order to check the generic type, we need to create an instance of the generic class and then we can compare the same with our class.ExampleFor this example, we will create a generic class of type and we will pass our variable inside this class in order to check its type.class MyGenericClass(val myclass: Class) {    companion object {       inline operator fun invoke() = MyGenericClass(T::class.java)   ... Read More

Kotlin equivalent of Java's equalsIgnoreCase

Soumak De
Updated on 16-Mar-2022 13:12:21
Java provides a String method called equalsIgnoreCase() which helps developers to compare two strings on the basis of their content. This comparison is case-insensitive, that is, it ignores whether the strings are in uppercase or lowercase and just compares the string values. In this article, we will see how we can implement the same functionality in Kotlin.Example – equalsIgnoreCase in JavaThe following example demonstrates how equalsIgnoreCase() works in Java.public class MyClass {    public static void main(String args[]){       String s1="TutorialsPoint";       String s2="tutorialspoint";       System.out.println("String 1: " ... Read More

How to create an instance of an abstract class in Kotlin?

Soumak De
Updated on 16-Mar-2022 13:03:45
When a class is defined in Kotlin with an abstract keyword, then it is known as an abstract class. In Kotlin, we cannot create an instance of an abstract class. Abstract classes can only be implemented by another class which should be abstract in nature. In order to use an abstract class, we need to create another class and inherit the abstract class.Example – Abstract class in KotlinThe following example demonstrates how you can create an instance of an abstract class in Kotlin.abstract class myInter {    abstract var absVariable : String    abstract fun absMethod() } class myClass : ... Read More

How to implement switch-case statement in Kotlin?

Soumak De
Updated on 16-Mar-2022 13:08:01
Switch case statement in any programming language is a type of selection control mechanism that allows the developers to test the value of a variable or expression and change the control flow of a program based on the outcome of the comparison. It also provides an option to do something whenever the value of the variable does not match a given value.Kotlin does not provide an option to write a switch-case statement; however we can implement the switch-case functionality in Kotlin using the when() function which works exactly the same way switch works in other programming languages.In this article, we ... Read More

How to add an item to an ArrayList in Kotlin?

Soumak De
Updated on 16-Mar-2022 12:55:22
In this example, we will see how we can define an ArrayList in Kotlin and add an item in the list. We can do it using the library function add() or we can use the "+=" operator. In order demonstrate, we will be creating two ArrayLists, one is of mutable type and the other is of immutable type.Example – Inserting a new item using add()We can insert an item to an ArrayList using the add() function provided by the Kotlin library. In this example, we will be creating two lists: one is "myMutableList" which is a collection of mutable data ... Read More

How to correctly concatenate strings in Kotlin?

Soumak De
Updated on 16-Mar-2022 12:50:36
There are different ways to concatenate strings in Kotlin. For example, we can use the $ operator or we can use the append() function or we can simply use the "+" operator to join two strings.Example – Concatenate Using "$"Kotlin provides an operator to reference a variable or expression. In this example, we will see how to concatenate two strings using the "$" operator.fun main(args: Array) {    val a = "Tutorialspoint"    val b = ".com"    println("String 1: " + a)    println("String 2: " + b)    println("Concatenated String: " + "$a$b") }OutputIt will produce the following ... Read More

How to catch many exceptions at the same time in Kotlin?

Soumak De
Updated on 16-Mar-2022 12:43:47
Exception handling is an important feature of any programming language. It restricts our programs from generating incorrect output at runtime. Kotlin does not support throwing multiple exceptions at the same time, however we can implement this functionality using some other functions provided by the Kotlin library.Example – Throwing Multiple ExceptionsIn this example, we will try to generate an arithmetic exception at runtime and we will try to catch the same in the catch block along with other different exception checks.fun main(args: Array) {    val item=0    var result=0    try {       result=item/0 // Exception: Division by ... Read More
Advertisements