I have seen the below validation. Could someone explain me what exactly does it implies or the use cases why we are going for it | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I have seen the below validation. Could someone explain me what exactly does it implies or the use cases why we are going for it

If( null ! = request) { } Is the above validation is same as If( request != null) ?

27th Aug 2021, 4:39 AM
!v@R
!v@R - avatar
1 Answer
+ 1
Yes, the two examples have the same result, it does not matter which expression is on the left or right side of the != comparison operator (at least in this case). In Java you can declare variables / class properties without initializing a value. Like: public String word; In that case, the initial value of the object will be null. If you want to do any operation on an actual value or object instance, you WANT to be sure that it is not null. Otherwise you may get runtime exceptions. The NullPointerException is sometimes called the "billion dollar mistake of the Java language" and it is a strong motivation for more modern languages such as Kotlin, to promote built-in null safety - precisely so the developer can avoid such null comparisons, because he can rely on the type system to do this automatically at compile time.
27th Aug 2021, 4:50 AM
Tibor Santa
Tibor Santa - avatar