官术网_书友最值得收藏!

Null safety issue

Accessing a member variable of a null reference will result in a null reference exception. In Java, this is NullPointerException. Java allows us to assign a null value to any variable that we declare, but when we try to use an object reference that has a null value, it throws NullPointerException. This is a common problem to do with programming languages. The language allows us to assign a null reference to a member variable, while also allowing us to access that variable without checking whether it is initialized. It would be preferable if the compiler reports such issues. This is exactly what Kotlin compiler does for us. 

In Kotlin, all the types are non-nullable by default. If we try to assign or return a null in the code, it fails to compile. The compiler differentiates between nullable references and non-null references, and the language provides an elegant syntax for null checks. This makes it almost impossible to encounter NPEs in Kotlin. In fact, if we do encounter an NPE, it is likely to be because we explicitly asked Kotlin to throw one, or because the NPE originates from external Java code.

主站蜘蛛池模板: 岳阳市| 湘阴县| 陇南市| 平顺县| 赤水市| 呈贡县| 禄丰县| 特克斯县| 称多县| 阆中市| 湖北省| 南江县| 中牟县| 湘潭县| 乐清市| 乐亭县| 玛曲县| 福海县| 册亨县| 阜南县| 凤凰县| 新龙县| 西吉县| 介休市| 武城县| 白沙| 库伦旗| 博兴县| 仁寿县| 武冈市| 盐城市| 凤庆县| 平远县| 淄博市| 河北区| 昭平县| 太仆寺旗| 承德市| 浦北县| 乌拉特后旗| 二连浩特市|