Found 417 Articles for Kotlin

Difference between "*" and "Any" in Kotlin generics

Soumak De
Updated on 23-Nov-2021 07:09:47

2K+ Views

In any programming language, generics are a powerful feature using which developers can create custom data type to manipulate a program in a different manner. There are many ways in which we can define generics in Kotlin.In this article, we will demonstrate the difference between "*" and "Any" keywords in Kotlin.When we define a collection with "*", it should contain the object of only that type. There should not be any mix and match between the data types inside a collection.If we use "Any", we can mix and match the data types, which means we can have multiple data types ... Read More

How to check "instanceof" class in Kotlin?

Soumak De
Updated on 23-Nov-2021 07:04:40

2K+ Views

Kotlin is a cross-platform, statistically typed, general-purpose programming language. It is very popular among the developer community because of its interoperable nature with JVM. In the programming world sometimes it is required to check the type of an object to implement a business logic.Unlike Java, we don't have an "instance of" operator in Kotlin. However, we have an "is" operator in Kotlin for type checking and casting.ExampleThe following example demonstrates how the "is" operator works in Kotlin.fun main(args: Array) { val x: String = "TutorialsPoint" // checking the instance and matching the type ... Read More

What is the equivalent of Java static final fields in Kotlin?

Soumak De
Updated on 23-Nov-2021 07:01:54

531 Views

"static" is a Java keyword that helps developers to define a class member, whereas the keyword "final" is used to declare a constant variable in Java. Once a variable is declared as static in Java, the value of the variable remains unchanged in every instance of the object. Similarly, once a variable is declared as final, its value cannot be altered.In this article, we will see how we can implement the same concept using Kotlin library function.Example - using companion objectA "companion object" is an object which is declared in the same file as a class. Both the class and ... Read More

How to clone or copy a list in Kotlin?

Soumak De
Updated on 23-Nov-2021 06:58:48

1K+ Views

A list is a type of collection in which data is stored in a sequential manner. We might come across a situation where the contents of one List have to be copied into another. In this article, we will see how to use Kotlin's built-in methods to clone a list.Example - Using toList()toList() is the basic method which can convert a collection into a list. We can use this method to clone an existing list too.fun main(args: Array) {    val x = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9)    val copiedList=x.toList();    println("Given collection: " +x) ... Read More

Difference between "fold" and "reduce" in Kotlin

Soumak De
Updated on 23-Nov-2021 06:54:27

936 Views

Kotlin is a cross-platform and statically typed general-purpose programming language. Kotlin provides many optional methods to traverse through the collections. fold() and reduce() are two different methods, both are helpful for traversing a collection. In this article, we will see how and when to use these two methods.Example – fold()If we want to traverse through a collection serially, then we can use fold().fold() takes one initial value and one operation to perform the operation on the initial value.There are different kinds of fold(), for example, foldRight() folds from right to left. By default, fold() will traverse from left to right.The ... Read More

Try-with-resources in Kotlin

Soumak De
Updated on 23-Nov-2021 06:51:34

521 Views

Kotlin is very efficient in managing the memory. Unlike Java, developers in Kotlin need not have to manage the memory explicitly. We do have different kinds of memory management techniques and Try-with-resource is one of them. In Kotlin, we have a function called 'use' which takes the burden of managing the resources automatically. This is a part of std library function provided by Kotlin.As per Kotlin documentation, use() is defined as a generic extension on all closeable types. The implementation looks like this −public inline fun T.use(block: (T) -> R): R {}In the above function, the definition block is the ... Read More

'break' and 'continue' in forEach in Kotlin

Soumak De
Updated on 23-Nov-2021 06:48:01

5K+ Views

In Kotlin, we have three types of structural jump expressions: "break", "return", and "continue". In this article, we will see how break and continue work in Kotliln.Break - This is a keyword that helps in terminating an iteration, once a given condition is met, while traversing through a collection.Continue - This keyword helps to continue the iteration once a condition is fulfilled.In Kotlin, we cannot explicitly use break and continue statements explicitly inside a forEach loop, but we can simulate the same action. In this example, we will see how to do it.Example: Return at labels :: directly to the ... Read More

Swift "if let" statement equivalent in Kotlin

Soumak De
Updated on 23-Nov-2021 06:28:11

2K+ Views

Swift "if let" is a conditional check operator to check whether a reference variable is NULL or not. This is a very useful technique to evaluate the unwrap optional value using swift language.In Kotlin, we can't use this operator directly. Instead, we will be using "let" and "run" to evaluate the same. In the following example, we will see how to use "let" and "run" using Kotlin library class.Example: Null Check using 'let' and 'run'In this example, we will check whether the value of a reference variable is NULL or not. If the value of the reference variable is NULL, ... Read More

How to implement Builder pattern in Kotlin?

Soumak De
Updated on 23-Nov-2021 06:17:19

738 Views

In object-oriented programming, the creation of objects is very easy via constructors when the object definitions are simple, but sometimes, it may so happen that the constructors need more variables or functions in order to initialize an object. In such cases, the "builder pattern" comes into picture which helps programmers to create small units of different objects and create a final object.A builder pattern provides an API to construct an object step- by-step.Builder patterns are particularly useful when objects need to be created dynamically.Note that it is not recommended to use builder patterns in Kotlin because we can get the ... Read More

How to convert a Kotlin source file to a Java source file?

Soumak De
Updated on 23-Nov-2021 06:13:27

610 Views

Kotlin is a statistically typed language that runs on JVM. Once a Kotlin file is compiled, it creates a .class file that can be executed on the JVM. In this article, we will see how we can convert a Kotlin source file to a Java source file. In this process, we will be taking help from different online Decompilers available on the Internet.Open VS Code.Go to the "extension" section and install "Kotlin language support for VS Code" and "Code Runner". We need these two extensions to run Kotlin in VS code environment.Install Kotlin compiler in your system as per the ... Read More

Advertisements