1. Abnormal error handling
/** * A protection against code that may execute exceptions * default Exception class :Exception ** /
fun main(args: Array<String>) {
println("-------------- direct display error --------------")
// Display the error directly
try{
"abc".toInt()
}catch (e:Exception){
println(e)
}
println("-------------- Ignore error --------------")
// Ignore errors
val a : Int ? = try {
"abc".toInt()
}catch (e: Exception){
null
}
println(a)
}
Copy the code
Integer: -1
0E is the safe converted c, and the value isnull
Copy the code
2. Type checking and conversion
Empty type
There are nullable and non-nullable types of any type
- val notNull:String = null // Error, cannot be null
- val nullable:String? = null // Yes, it can be null
- notNull.length // True, non-null values can be used directly
- nullable.length // Error, possibly null, cannot get length directly- nullable!! .length// Nullable cannot be nullable forcibly- nullable? .length// If nullable is null, return null
Copy the code
fun getName(a): String? {
return "Zhang"
}
fun getName2(a): String? {
return null
}
fun main(args: Array<String>) {
valname = getName() ? :return
println("Name 1:$name")
val value: String? = "Hello World"
// Do not check for nullprintln(value!! .length)valname2 = getName2() ? :return
println("Name 2:$name2")}Copy the code
The name1: zhang SAN11
Copy the code
Intelligent type conversion
Java Style type conversion
- val sub:SubClass = parent asSubClass - A Java-like type conversion that throws an exception on failureCopy the code
Safe type conversion
- val sub:SubClass? = parent as? SubClass - Returns if conversion failsnull, do not throw exceptionsCopy the code
/** * check: To convert a variable to another type use */
fun main(args: Array<String>) {
val a = 5
val b = 6
val c = if (a > b) "More than" else a - b
// Type judgment: is
if (c is String) {
println(c.length)
} else {
println("Integer:$c ")}// The Kotlin compiler converts intelligently most of the time
if(c is Int) println(c.inc())
/ * * * manual conversion: casting as * casting complains. Java lang. ClassCastException * * /
//val d = c as String
//println("d is cast c, value $d ")
/** * Manual conversion: safe conversion as? * Secure conversion */
val e = c as? String
println("E is the safe converted c, and the value is$e ")}Copy the code
**
Integer: -1
0E is the safe converted c, and the value isnull
Copy the code
Intelligent type conversion
- if(parent isSubClass)parent.< member of a SubClass > - The compiler deduces the type as much as possible, and avoids useless type conversionsif(nullable! =null) nullable. Length - Correct! Because we confirm nullableCopy the code
public class Parent {
public String getName(){
return "parent"; }}Copy the code
public class Child extends Parent{
@Override
public String getName() {
return "child"; }}Copy the code
fun main(args: Array<String>) {
val parent: Parent = Child()
if (parent is Child) {
println(parent.name)
}
val value: String? = "Hello World"
/ / because the value! = NULL automatically String
if(value ! =null) {
println(value.length)
}
}
Copy the code
child
11
Copy the code