We have to find strategies that help us avoiding unwanted bugs due to NPEs. Wouldn't it actually be nice if we were able to identify the method's returned Device type as nullable (or non-nullable respectively) firsthand? Null-Safety in Java Not knowing about the possibility of a returned null reference is going to cause an awful NullPointerException in the subsequent call to getName. Well documented methods will describe exactly that behavior, which still requires the developer to be very attentive. The method getAudioDevice returns an object of type Device but might return null in order to denote the absence of that device on particular systems. Have a look at the following code example: Device audio = new DeviceProvider().getAudioDevice() Although Java relies on strong static typing, it doesn't let you distinguish between reference types that can and cannot hold a null reference. The null reference is often used to indicate absent values, which isn't obvious to the programmer in many cases. This almost always happens unintentionally in consequence of a bug, which is based on unrecognized references to null. After that, the article demonstrates how Kotlin nullability features work and how they improve your code.Īs Java developers, we're very accustomed to NullPointerExceptions (NPE) that are thrown at the runtime of an application. In this article, we will review the problems that may be caused by null pointers and how to avoid them in Java. By s1m0nw1 2 Comments Null-Safe Programming - The Kotlin Wayĭisclaimer: This ktor article was originally published in the Dzone Java Guide 2018, which can be downloaded here.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |