科特林中的“?:”用於什麼?(艾爾維斯運算子)
艾爾維斯運算子在許多程式語言中非常常見。這是一個二元表示式,當表示式的值為真時返回第一個運算元,而當表示式的值為假時返回第二個運算元。通常,艾爾維斯運算子用“?:”表示,語法如下 −
First operand ?: Second operand
示例
以下示例演示瞭如何在科特林中使用艾爾維斯運算子。
fun main(args: Array<String>) {
val x: String? = null
val y: String = x ?: "TutorialsPoint.com"
// it will check whether the value of x
// is NULL or not. If NULL, then
// it will return y, otherwise x
println(x ?: y)
}輸出
在上面的示例中,艾爾維斯運算子將檢查“x”的值是否為 NULL。如果是,則返回第一個運算元“x”,否則返回“y”。根據我們的示例,它將返回“y”,因為“x”的值為 NULL。
TutorialsPoint.com
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP