dir.by  
  Search  
Programming, development, testing
Kotlin
null value, use the symbol ? and ?. and !! and ?: and !!. and ?. in Kotlin
  Looked at 973 times    
 null value, use the symbol ? and ?. and !! and ?: and !!. and ?. in Kotlin 
last updated: 2 February 2025
 ? null  To specify an empty value in a variable
Add the ? character to the type and assign the empty value null
  Kotlin  
var bookPrice:Int? = null
  !!   Take a value from a variable with an exception
Add !! to take the value.
If we use !! and the value is null, then a exception is thrown.
  Kotlin  
var bookPrice1:Int? = 10
var bookPrice2:Int? = null
var bookPrice3:Int? = 7

var allPrice:Int = bookPrice1!! + bookPrice2!! + bookPrice3!!
Note!
In my example, allPrice is not calculated because bookPrice2!! will throw an exception.
  ?:   Take a value from a variable without exception
Add ?: to take the value.
If we use ?: and the value is null, then we set the value ourselves.
  Kotlin  
var bookPrice1:Int? = 10
var bookPrice2:Int? = null
var bookPrice3:Int? = 7

var allPrice:Int = (bookPrice1?:0) + (bookPrice2?:0) + (bookPrice3?:0)
In my example, allPrice = 17
  !!.   Call a class method with an exception
Add !!. to call the method.
If we use !!. and the value is null, then a exception is thrown.
  Kotlin  
class MyBook
{
     fun Show()
     {
          println("Hello")
     }
}

fun main() {
     var b1:MyBook? = null

     b1!!.Show()
}
In my example, the Show method won't be called because b1!!. will throw an exception.
  ?.   Call a class method without exception
Add ?. to call the method.
If we use ?. and the value is null, then the method will not be called. There will be no exception.
  Kotlin  
class MyBook
{
     fun Show()
     {
          println("Hello")
     }
}

fun main() {
     var b1:MyBook? = null

     b1?.Show()
}
 
← Previous topic
Create collections list, set, map and an array array in Kotlin
 
Next topic →
Lambda function in Kotlin. Example 1: var myFunc1 : (a:Int, b:Int) -> Int = { p1, p2 -> p1 + p2 };
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

  Объявления  
  Объявления  
 
What is Kotlin?
Why is IntelliJ IDEA the most popular development environment for Kotlin?
Download and install IntelliJ IDEA to explore Kotlin
Create a new project in IntelliJ IDEA for study Kotlin
Int, Float, Boolean, Char ...
Integer numbers in Kotlin are: Byte, UByte, Short, UShort, Int, UInt, Long, ULong
Decimal numbers in Kotlin are: Float, Double
A flag with values of true or false in Kotlin is: Boolean
The symbol in Kotlin is: Char
Convert number to text in Kotlin | Int → String
String
The string, the text in Kotlin is: String
Interpolate strings in Kotlin. Example: val address:String = "${street}, ${country}"
What is the difference between String and StringBuilder ?
Enum
What is an enumeration (enum) in Kotlin ?
How do I find enum by value in Kotlin ?
Class
What is class in Kotlin? Example: class MyBook { ... }
lateinit is a late initialization for the class field | Kotlin
class that inherits from interface in Kotlin | Example: class MyBook : IBook { ...}
Unnamed class that inherits from interface in Kotlin | Example: val book1 = object : IBook { ...}
Generic, template class in Kotlin | Example: class MyBook<T> { ... }
Collections and Arrays
Create collections list, set, map and an array array in Kotlin
null
null value, use the symbol ? and ?. and !! and ?: and !!. and ?. in Kotlin
Lambda function
Lambda function in Kotlin. Example 1: var myFunc1 : (a:Int, b:Int) -> Int = { p1, p2 -> p1 + p2 };

  Ваши вопросы присылайте по почте: info@dir.by  
Яндекс.Метрика