Articles on Trending Technologies

Technical articles with clear explanations and examples

What does "return@" mean in Kotlin?

Soumak De
Soumak De
Updated on 16-Mar-2022 4K+ Views

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
Soumak De
Updated on 16-Mar-2022 6K+ Views

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
Soumak De
Updated on 16-Mar-2022 1K+ Views

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
Soumak De
Updated on 16-Mar-2022 5K+ Views

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
Soumak De
Updated on 16-Mar-2022 2K+ Views

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 implement switch-case statement in Kotlin?

Soumak De
Soumak De
Updated on 16-Mar-2022 9K+ Views

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 correctly concatenate strings in Kotlin?

Soumak De
Soumak De
Updated on 16-Mar-2022 9K+ Views

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

Creating POJO Class for Kotlin

Soumak De
Soumak De
Updated on 16-Mar-2022 3K+ Views

Kotlin has been developed over JVM and hence it is fully compatible with JVM. Java POJO class stands for Plain Old Java Object (POJO) which is used to hold the data.In Java, along with defining the variables, we need to create different supporting methods in order to access those private members of the class.But Kotlin provides a unique way to declare a POJO class by introducing the "data" keyword. It can be applied along with class.Once a class is defined as data class, the Kotlin compiler itself will be creating all the supporting getter() and setter() methods for this class.Example ...

Read More

Getters and Setters in Kotlin

Soumak De
Soumak De
Updated on 16-Mar-2022 721 Views

Properties in Kotlin can be declared either as mutable using the "var" keyword or as read-only using the "val" keyword. Both these types of variables can be referred by their respective names after the method declaration.In Kotlin, getter() and setter() methods need not be created explicitly. The Kotlin library provides both of them by default.ExampleIn this example, we will see how to use the getter() and setter() methods in Kotlin.fun main(args: Array) {    // getter()    println("Name is -> " + Address().name)    println("City is -> " + Address().city)    println("State is -> " + Address().state) } class Address ...

Read More

Difference between isNullOrEmpty and isNullOrBlank in Kotlin

Soumak De
Soumak De
Updated on 16-Mar-2022 2K+ Views

Both these functions, isNullOrEmpty and isNullOrBlank, are used in Kotlin whenever it is required to check if a String value is empty or not. Let's check how these two functions are different from each other.isNullOrBlank – It takes whitespaces into account, which means " " is different from "". This function will return True only when the String is declared with no characters in it. It will check whether the value of the String is NULL and it will also check whether the String is blank.isNullOrEmpty() – This function checks whether the string is declared as NULL or whether it ...

Read More
Showing 34181–34190 of 61,248 articles
Advertisements