Mobile Development Articles

Page 22 of 156

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

Soumak De
Soumak De
Updated on 23-Nov-2021 905 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
Soumak De
Updated on 23-Nov-2021 2K+ 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
Soumak De
Updated on 23-Nov-2021 1K+ 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
Soumak De
Updated on 23-Nov-2021 741 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

Swift "if let" statement equivalent in Kotlin

Soumak De
Soumak De
Updated on 23-Nov-2021 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
Soumak De
Updated on 23-Nov-2021 926 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
Soumak De
Updated on 23-Nov-2021 875 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

Convert Kotlin Array to Java varargs

Soumak De
Soumak De
Updated on 23-Nov-2021 345 Views

Varargs, also known as "variable arguments" is a new mechanism in Java by which methods in Java can accept zero or multiple number of arguments. Before this mechanism, the only option available to achieve this type of functionality was "method overloading", but that also required multiple lines of boilerplate code.In this article, we will see how we can use Varags in Kotlin to call a function multiple times based on different types of arguments. The following example demonstrates how we can use this Varags keyword.Examplefun main() {    // calling the function with 4 arguments and    // passing 3 ...

Read More

Accessing Kotlin extension functions from Java

Soumak De
Soumak De
Updated on 23-Nov-2021 2K+ Views

In Kotlin, you can easily call another function by just importing the same in the current Kotlin file where your main function is running. Whatever the function we are declaring in a Kotlin file will be compiled into a static method by default, and it will be placed under the same package. The name of the newly created file will be renamed as the First letter capitalized and ".kt" extension replaced with the "Kt" suffix.In this article, we will try to get an insight into how you can use Kotlin extension methods from a Java file.ExampleLet's create a Kotlin file ...

Read More

How to call a function after a delay in Kotlin?

Soumak De
Soumak De
Updated on 27-Oct-2021 3K+ Views

Kotlin is based on Java, hence we can use Java-based library functions to delay a function call. In this article, we will be using a Java library function to delay the function call using Timer() and schedule().Exampleimport java.util.Timer import kotlin.concurrent.schedule fun main(args: Array) {    // Execution starting point    println("Hello world!!")    // Delay of 5 sec    Timer().schedule(5000){       //calling a function       newMethod()    } } fun newMethod(){ println("Delayed method call!") }OutputOnce executed, the above piece of code will yield the following output −Hello world!! Delayed method call!

Read More
Showing 211–220 of 1,551 articles
« Prev 1 20 21 22 23 24 156 Next »
Advertisements