Found 417 Articles for Kotlin

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

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

781 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

How to throw custom exception in Kotlin?

Soumak De
Updated on 01-Mar-2022 10:33:05

269 Views

Exception is an important aspect of any programming language. It prevents our code from generating incorrect output at runtime. There are two types of exceptions −Checked exceptionsUnchecked exceptionsChecked ExceptionsChecked exceptions are those which are checked at the compile time. As per example, FileNotFoundException() or IOException. In the following example, we will see how we can generate a checked Exception.Exampleimport java.io.File import java.io.InputStream fun main(args: Array) {    try {       val inputStream: InputStream = File("Hello.txt").inputStream()    } catch(e:Exception) {       e.printStackTrace();    } }OutputOnce you execute this code, it will generate the following output in ... Read More

Override getter for Kotlin data class

Soumak De
Updated on 01-Mar-2022 09:59:38

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

Best way to null check in Kotlin

Soumak De
Updated on 04-Oct-2023 21:25:58

23K+ Views

In any programming paradigm, it is mandatory to check "null safety" in order to prevent runtime errors. In this article, we will see different ways to check "null safety" in Kotlin. Example - Using if…else In most of the programming languages, we have the "if" keyword to check conditions. In Kotlin too, we can use the "if-else" clause to check the null safety of a variable. fun main() { val name="TutorialsPoint.com" //null check if (name != null) { println(name) ... Read More

What does 'by' keyword do in Kotlin?

Soumak De
Updated on 23-Nov-2021 07:25:10

3K+ 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
Updated on 23-Nov-2021 07:23:31

873 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
Updated on 23-Nov-2021 07:18:26

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

What does "?:" do in Kotlin? (Elvis Operator)

Soumak De
Updated on 23-Nov-2021 07:16:42

531 Views

Elvis operator is very common in many programming languages. This is a binary expression that returns the first operand when the expression value is True and it returns the second operand when the expression value is False. Generally, the Elvis operator is denoted using "?:", the syntax looks like −First operand ?: Second operandExampleThe following example demonstrates how you can use the Elvis operator in Kotlin.fun main(args: Array) { val x: String? = null val y: String = x ?: "TutorialsPoint.com" // it will check whether the value of x ... Read More

Difference between a "class" and an "object" in Kotlin

Soumak De
Updated on 23-Nov-2021 07:14:45

283 Views

Kotlin is a statistically typed language. It has been built over Java, hence it inherits all the object-oriented programming concepts of Java. In this article, we will see the difference between a "class" and an "object" in Kotlin.A "class" is a blueprint of a runtime entity and an "object" is its state, which includes both its behavior and state. In Kotlin, class declaration consists of a "class header" and a "class body" surrounded by curly braces, similar to Java. A general-purpose class definition looks like this −// class Header Class myClass { // class Body }There are ... Read More

When to use an inline function in Kotlin?

Soumak De
Updated on 23-Nov-2021 07:12:39

6K+ Views

Kotlin is a statistically typed language. It has different options to handle higher-order functions. Kotlin came up with a wonderful solution for higher-order functions by introducing inline functions.An Inline function is a kind of function that is declared with the keyword "inline" just before the function declaration. Once a function is declared inline, the compiler does not allocate any memory for this function, instead the compiler copies the piece of code virtually at the calling place at runtime.You should opt for an inline function in Kotlin in the following situations −When you need to access higher-order functions.When you need to ... Read More

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