Welcome Guest, Not a member yet? Register   Sign In
Java equals() selection
#1

(This post was last modified: 11-14-2022, 01:16 AM by leusiam. Edit Reason: Added reference )

In Java, if I try to do.equals() on a null string, a null pointer error is issued. I’m wondering whether I can perform the following if I’m attempting to compare if a string is equal to a constant string:
Code:
MY CONSTANT STRING.equals(aStringVariable)

I’m sure it’ll work, but is this simply extremely bad code? Also, is it possible to convert string to array in this situation like said here? This is a common Java idiom known colloquially as a Yoda condition. Personally, I prefer to handle the null situation directly, but the Yoda method is widely used, and any competent Java programmer should quickly grasp what is going on. How should I proceed?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB