No, it has no purpose whatsoever in Java.
In C and some of its related languages, it was sometimes used to avoid making this mistake:
if (someVariable = null)
Note the =
rather than ==
, the author has inadvertently assigned null
to someVariable
rather than checking for null
. But that will result in a compiler error in Java.
Even in C, any modern compiler will have an option to treat the if (someVariable = null)
as a warning (or even an error).
Stylistically, I agree with you — I wouldn't say "if 21 you are, I will serve you a drink" (unless I'd already had a couple several and was doing my Yoda impersonation). Mind you, that's English; for all I know it would make perfect sense in other languages, in which case it would be perfectly reasonable style for speakers of those languages.