What's the difference between !! and ? in Kotlin?

后端 未结 6 1303
情深已故
情深已故 2020-12-04 14:15

I am new to Kotlin. I want to know the difference between this two !! and ? in below code.

Below, there are two snippets: the first uses

6条回答
  •  抹茶落季
    2020-12-04 14:55

    In Addition to what Alexander said and as shown in the docs too, 
    

    the ?. safe call operator is very useful in chaining, something like this

    student?.department?.hod?.name
    

    if there is no student, returns null otherwise look for his department. If department doesn't exist returns null otherwise look for hod (head of department) and so on.

    If any one of student, department or hod is null then the result will be null.

提交回复
热议问题