Found 417 Articles for Kotlin

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

Soumak De
Updated on 16-Mar-2022 13:03:45

3K+ Views

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

7K+ 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 add an item to an ArrayList in Kotlin?

Soumak De
Updated on 16-Mar-2022 12:55:22

4K+ Views

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

7K+ 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

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

Soumak De
Updated on 16-Mar-2022 12:43:47

2K+ Views

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

How to create an array in Kotlin like in Java by just providing a size?

Soumak De
Updated on 16-Mar-2022 12:38:33

809 Views

Kotlin is a cross platform statistically typed language based upon JVM. Kotlin is designed in such a way that it is interoperate fully with Java and JVM. In Java, we can simply create an array by providing a size.Example – Array of Specific Size in JavaThe following example demonstrates how to create an array of a specific size in Java.public class MyClass {    public static void main(String args[]) {       int a[]=new int[5];       for(int i=0;i

How to create an instance of an anonymous interface in Kotlin?

Soumak De
Updated on 16-Mar-2022 12:21:28

2K+ Views

Kotlin has been developed over JVM, hence it supports most of the features of JVM. Java provides a feature called anonymous inner classes to handle the cases where we have to create an object of a class with a slight modification, without declaring a new subclass. An anonymous inner class doesn't have a name; we define it directly at the instantiation line.However, Kotlin uses object expressions to provide the same sub-class functionality. In Kotlin, we can create an object expression of an interface by implementing its abstract methods. This implementation technique is known as anonymous interface.Example – Anonymous Interface in ... Read More

Creating POJO Class for Kotlin

Soumak De
Updated on 16-Mar-2022 12:17:12

2K+ 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
Updated on 16-Mar-2022 12:12:45

547 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
Updated on 16-Mar-2022 12:09:22

1K+ 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

Previous 1 ... 4 5 6 7 8 ... 42 Next
Advertisements