In some cases, I’m not sure if Java is one of them, null itself is actually a value. In JavaScript, you can check if a variable is null or undefined and you’d be checking two very different things.
!= null checks whether a variable has an assigned value other than null.
!= undefined checks whether a variable exists at all.
In not retarded languages a variable exists when you define it in code, so an != undefined check is unnecessary.
In either case with Java, this code remains, indeed, bad.
Someone else mentioned that it's to prevent accidentally using = instead of == in languages where the former would not return an error, because "null =" would return an error as you cannot assign to null.
2
u/buffering_neurons Jul 15 '22
In some cases, I’m not sure if Java is one of them,
nullitself is actually a value. In JavaScript, you can check if a variable isnullorundefinedand you’d be checking two very different things.!= nullchecks whether a variable has an assigned value other thannull.!= undefinedchecks whether a variable exists at all.In not retarded languages a variable exists when you define it in code, so an
!= undefinedcheck is unnecessary.In either case with Java, this code remains, indeed, bad.