

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What's the difference between "!!" and "?" in Kotlin?
In this article, we will take an example and demonstrate the difference between (!!) and (?) in Kotlin.
Example – "!!" and "?" operator in Kotlin
Kotlin 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<String>) { val nullValue: String ?=null // it will print null println("The value is ->"+nullValue?.length) // it will throw the exception println(nullValue!!.length) }
Output
On execution, it will produce the following output −
The value is ->null Exception in thread "main" java.lang.NullPointerException at MainKt.main(main.kt:8)
The following table sums up the difference −
Input | <<Val>>?.length | <<Val>>!!.length |
Input is null | null | Null pointer exception |
- Related Questions & Answers
- What's the difference between sizeof and alignof?
- What's the difference between window.location and document.location?
- What's the difference between Matplotlib.pyplot and Matplotlib.figure?
- What's the difference between Tkinter's Tk and Toplevel classes?
- What's the difference between lists and tuples in Python?
- What's the difference between RSpec and Cucumber in Selenium?
- Difference between isNullOrEmpty and isNullOrBlank in Kotlin
- Difference between char s[] and char *s in C
- What is the difference between Python's re.search and re.match?
- Difference Between PGP and S/MIME
- Difference between List and Array types in Kotlin
- What's the difference between nohup and ampersand (&) on Linux?
- What is the difference between "var" and "val" in Kotlin?
- What is the difference between "const" and "val" in Kotlin?
- What's the difference between assignment operator and copy constructor in C++?
Advertisements