What programming languages ​​prevent null pointer exceptions?

I know that you cannot get a null pointer exception in Haskell, but I'm curious that other languages ​​have this function. For example, a colleague asked if any interpreted languages ​​were forbidden, but I did not know the answer. The only language I know about this helps to avoid a billion dollar error - Haskell.

+4
source share
2 answers

In Objective-C, you can pass messages to objects that are actually nil , and they silently return nil themselves.

But you can still risk null pointer exceptions when you dereference pointers to C (for example, when using * or . Operators).

+3
source

Kotlin is a statically typed programming language that runs on a Java virtual machine and can also be compiled into JavaScript source code or use the LLVM compiler framework.

Kotlin avoids null pointer exceptions by distinguishing between NULL and NULL types. All variables with a null value should be declared with ?? postfix after the type name. Operations with variables with a null value need special care from the developers: before using the value, you must perform a null check.

To call methods on a variable of type NULL, you must either use "?" an operator that only calls a method if the variable is not null (and makes it a nullable return type), or "!!" An operator that claims that a variable is not currently zero.

Please note that you can still get Kotlin exceptions with exceptions if you use "!!" operator on a null object or when interacting with Java.

0
source

Source: https://habr.com/ru/post/1493758/


All Articles