Mobile Development Articles

Page 23 of 156

Print 0001 to 1000 in Kotlin with padding

Soumak De
Soumak De
Updated on 01-Mar-2022 237 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

Read More

How to remove an item from an ArrayList in Kotlin?

Soumak De
Soumak De
Updated on 01-Mar-2022 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

Equivalent of getClass() for KClass in Kotlin

Soumak De
Soumak De
Updated on 01-Mar-2022 339 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
Soumak De
Updated on 01-Mar-2022 754 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
Soumak De
Updated on 01-Mar-2022 1K+ 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

What's the difference between "!!" and "?" in Kotlin?

Soumak De
Soumak De
Updated on 01-Mar-2022 1K+ Views

In this article, we will take an example and demonstrate the difference between (!!) and (?) in Kotlin.Example – "!!" and "?" operator in KotlinKotlin provides a wonderful operator to check NULL pointer exceptions. It throws a NULL pointer exception instead of breaking the programming logic whenever the variable is NULL.In the following example, the value of "test" is NULL. Hence, Kotlin will throw a NULL pointer exception instead of breaking down the logic. The example shows the different uses of "!!" and "?" operators.fun main(args: Array) {    val nullValue: String ?=null    // it will print null ...

Read More

Override getter for Kotlin data class

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

Data class is a collection in Kotlin to store data. Logically this is same as creating a Java POJO class. In Java, we do create extra member functions to set and get the data in the member variables. In Kotlin, we don’t need to create separate methods to access the member variables of the class. Kotlin provides it by default. We can access the variables by just using the member variable names in it.It is not recommended to override any of the data class members like we used to do in Java.It is always recommended to use normal class if ...

Read More

What does 'by' keyword do in Kotlin?

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

Kotlin supports delegation of design pattern by introducing a new keyword "by". Using this keyword or delegation methodology, Kotlin allows the derived class to access all the implemented public methods of an interface through a specific object.ExampleIn this example, we will implement an abstract method of a Base class from another class.interface Base {    //abstract method    fun printMe() } class BaseImpl(val x: Int) : Base {    // implementation of the method    override fun printMe() { println(x) } } // delegating the public method on the object b class Derived(b: Base) : Base by b ...

Read More

What is "out" keyword in Kotlin?

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

"Out" keyword is extensively used in Kotlin generics. Its signature looks like this −ListWhen a type parameter T of a class C is declared out, then C can safely be a super type of C. That means, a Number type List can contain double, integer type list.ExampleThe following example demonstrates how you can use the "out" keyword in Kotlin −fun main(args: Array) {    var objet1 = genericsExample(10)    var objet2 = genericsExample(10.0) } // As generic type is declared as "out", // we can pass Int and Double also. class genericsExample(input:Any?) {    init {       ...

Read More

How to read a text file from resources in Kotlin?

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

In this article, we will see how we can read a text file using Kotlin library functions. Kotlin is based on Java, hence we can use different Java library functions in Kotlin too.Example - BufferedReaderGo ahead and create a Kotlin file in your workspace and name it " ReadFile.kt". Keep a text file with some data in the same directory. For this example, our Text file looks like this −Welcome to the best tutorial website - www.tutorialsPoint.com This is the Text file that we want to read via KotlinExecute the following piece of code to read the above text file.// ...

Read More
Showing 221–230 of 1,551 articles
« Prev 1 21 22 23 24 25 156 Next »
Advertisements