Found 417 Articles for Kotlin

How to convert TimeStamp to DateTime in Kotlin?

Soumak De
Updated on 01-Mar-2022 11:37:27

5K+ Views

Kotlin is a statistically typed language and it is based on Java, hence all the Java code can easily be compiled within Kotlin code. In this article, we will see how we can generate the current local date and time in Kotlin.As Kotlin is interoperable with Java, we will be using Java utility class and Simple Date Format class in order to convert TimeStamp into DateTime.Example – Converting DateTime using Java util classAs Kotlin is comparable with the JVM, we can use the Java util class in order to convert the TimeStamp into DateTime.import java.text.SimpleDateFormat import java.util.* fun main(args: ... Read More

Print 0001 to 1000 in Kotlin with padding

Soumak De
Updated on 01-Mar-2022 11:33:48

147 Views

In this example, we will see how to print 0001 to 1000 in Kotlin with padding. For this purpose, we will use a Kotlin library function called padStart().padStart is a function which returns a charSequence. Its function definition looks like this −fun CharSequence.padStart( length: Int, padChar: Char = ' ' ): CharSequenceExample – Print 0001 to 1000 with paddingThe following code prints 1 to 1000 with padding.fun main(args: Array) {    (1..1000).forEach{println("$it".padStart(4, '0'))} }OutputOn execution, it will print a sequence of 4-digit numbers starting from 0001 to 1000.0001 0002 0003 0004 ... ... ... 0996 0997 0998 0999 1000

How to print all the elements of a String array in Kotlin in a single line?

Soumak De
Updated on 01-Mar-2022 13:28:41

515 Views

In this article, we will take an example and show how to print all the elements of a String array in a single line using a Kotlin library class. In order to do that, we will use a String function called joinToString(), provided by the Kotlin library.As per the Kotlin documentation, the function definition looks like this −fun Array.joinToString(    // the String will be separated by this    separator: CharSequence = ", ",    // This will be added as prefix to the String    prefix: CharSequence = "",    // This will be added as postfix ... Read More

Equality checks in Kotlin (Difference between "==" and "===" Operators)

Soumak De
Updated on 01-Mar-2022 11:16:42

1K+ Views

Kotlin is statistically typed language and it is hundred percent comparable with Java, as it was developed based on JVM. In Kotlin, there are two types of equality checks −One is denoted by "==" andThe other one is denoted by "===".As per the official documentation, "==" is used for structural equality, whereas "===" is used for referential equality.For any expression, a==b will evaluate to True only when the value of both "a" and "b" are equal.a===b will evaluate to True only when both "a" and "b" are pointing to the same object.Example – Equality in KotlinIn this example, we will ... Read More

How to remove an item from an ArrayList in Kotlin?

Soumak De
Updated on 01-Mar-2022 11:13:33

2K+ Views

In this article we will see how we can remove an item from an ArrayList using Kotlin library function. In order to do that, we will take the help of a library function called drop(). The function definition looks as follows −fun Array.drop(n: Int): List (source)It takes array and a position as an input and it returns a list containing all the elements except the first n elements.Example – drop() in KotlinIn this example, we will remove the first element from a list using drop().fun main(args: Array) {    var arrayone: ArrayList = arrayListOf("mango", "jam", "apple", "lemon", "spice")   ... Read More

How to create a list in Kotlin?

Soumak De
Updated on 01-Mar-2022 11:09:32

954 Views

A list is a collection to hold same type of data in a single variable. Kotlin does not provide any dedicated literal to create a collection. As per the documentation of Kotlin, a List is an ordered collection with access to elements by indices.In Kotlin, we do have two different kinds of collections; one is read-only which is known as immutable collection and the other type of collection is where we can have a write facility as well which is known as mutable collection.In this article, we will see how we can create these two types of lists and manipulate ... Read More

How to create an empty array in Kotlin?

Soumak De
Updated on 01-Mar-2022 11:01:38

314 Views

An array is a collection where we can store multiple items of the same type. We can consider an array of integers or an array of strings. This is very useful, for example, whenever we need to store the name of 1000 students in a single variable.Example – Using arrayOf()In this example, we will see how we can create an empty array in Kotlin. We will be creating an empty array of Strings and manipulate the same in the program.fun main(args: Array) {    // Declareting empty array of type String    val emptyStringArray = arrayOf()    println("Example of empty ... Read More

Equivalent of getClass() for KClass in Kotlin

Soumak De
Updated on 01-Mar-2022 10:56:29

211 Views

In this article, we will take an example and demonstrate how we can obtain the class reference in Kotlin. Kotlin does not support fetching the class reference directly, but you can obtain the same reference via extension itself. In the following example, we will see how we can do it via Kotlin library functions.Example – Class reference using KClassIn this example, we will get the reference of the class.import kotlin.reflect.KClass fun main(args : Array) {    // to get the reference of the class    fun T.getClass(): KClass {       return javaClass.kotlin    }    val ... Read More

How to extend and implement at the same time in Kotlin?

Soumak De
Updated on 01-Mar-2022 10:51:36

587 Views

In this article, we will take an example to demonstrate how we can extend and implement in the same class. In this example, We will be creating an interface and a dummy Parent class.From the Child class, we will extend the Parent class and implement the interface.Example – Extending and implementing in childinterface myInterface {    fun test(): String } open class myParentClass(val name:String) {    fun anotherTest():String {       return name    } } class Child() : myInterface, myParentClass("ParentClass Input"){    //child extending the parent class and implementing myInterface    override fun test(): String { ... Read More

Why can't 'kotlin.Result' be used as a return type?

Soumak De
Updated on 01-Mar-2022 10:44:58

995 Views

Result is a Serializable class in Kotlin. The function definition looks like this −class Result : SerializableThis class has two properties − "isFailure" and "isSuccess".As per the documentation, Result cannot be used as a direct return type of Kotlin function. However, in this article, we will see how we can use this Result in our Kotlin programs.// ERROR: 'kotlin.Result' cannot be used as a return type fun findUserByName(name: String): Result fun foo(): Result      // ERROR fun foo(): Result?          // ERROR var foo: Result             // ERRORExample – Kotlin.Resultsealed class ... Read More

Previous 1 ... 6 7 8 9 10 ... 42 Next
Advertisements