科特林中的“?:”用于什么?(艾尔维斯操作符)
艾尔维斯操作符在许多编程语言中非常常见。这是一个二元表达式,当表达式的值为真时返回第一个操作数,而当表达式的值为假时返回第二个操作数。通常,艾尔维斯操作符用“?:”表示,语法如下 −
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