Skip to content
Advertisement

Java if statement is skipped

In the following Java code, the if statement conditional does not evaluate to true and its block is skipped.

public void options(String input)
{
    if(input == "x")
        System.exit(0);
}

Input has the right value, so why is the System.exit(0) skipped?

Advertisement

Answer

This is a classic. Don’t use “==” for comparing Strings, use String.equals().

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement