Article directory
1. Compound symbols
1. === !== (third class)
== 是比较值是否相等
=== 是比较对象的地址是否相等
2. !! (assert non-null operator)
!! 比如 var s = foo!! 如果foo不为null 就取出值,如果foo为null 就抛出异常
3. ? (whether the current object can be null)
4. ?. (safety caller)
code:
if (foo != null) {
return foo.bar()
} else {
return null
}
5. ?: (similar to java judgment)
6. is
val str = "helloWorld"
System.out.println("========")
println(str is String)//输出:true
System.out.println("========")
Output information:
I/System.out: ========
true
========
7、!is
val x = 10
System.out.println("========")
println(x !is Int)//输出:false
System.out.println("========")
Output information:
I/System.out: ========
false
========
8. as (type-unsafe converter) , if the conversion is successful, the conversion will be successful, and if the conversion fails, an error will be reported
private var view1: View? = null
private var view2: View? = null
private var view3: View? = null
private var mListView1: ListView? = null
private var mListView2: ListView? = null
private var mListView3: ListView? = null
...
view1 = inflater.inflate(R.layout.pager_number, null)
view2 = inflater.inflate(R.layout.pager_number, null)
view3 = inflater.inflate(R.layout.pager_number, null)
//as转换成功就转换成功
mListView1 = view1!!.findViewById<View>(R.id.listview) as ListView
mListView2 = view2!!.findViewById<View>(R.id.listview) as ListView
mListView3 = view3!!.findViewById<View>(R.id.listview) as ListView
9. as? (type safety converter) , as? is safer than as, if the conversion is successful, the conversion is successful, and if the conversion fails, it will return null
1. The output is not null
var name:String = "哈哈哈"
Log.e("TAG", (name as? String).toString())
Output information:
2. Output null
E/TAG: 哈哈哈
var name:String = "哈哈哈"
Log.e("TAG", (name as? Int).toString())
Output information:
E/TAG: null