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

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.

主站蜘蛛池模板: 沙田区| 双流县| 邳州市| 泸定县| 阿克陶县| 深泽县| 康定县| 黎川县| 永清县| 太仆寺旗| 碌曲县| 安溪县| 温泉县| 松江区| 营山县| 宜都市| 宜兰市| 乐平市| 威信县| 灯塔市| 临湘市| 萨嘎县| 北票市| 庆安县| 漳州市| 新建县| 阿克陶县| 武威市| 尼玛县| 西安市| 隆尧县| 高陵县| 邵武市| 尖扎县| 渑池县| 石台县| 治多县| 沙洋县| 宁波市| 汉中市| 建德市|