• Java equals() selection

    Tags:

    Author
    Topic
    #2496636

    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:

    MY CONSTANT STRING.equals(aStringVariable)

    I’m sure it’ll work, but is this simply extremely bad code?
    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?

    Viewing 1 reply thread
    Author
    Replies
    Viewing 1 reply thread
    Reply To: Java equals() selection

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: