Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't my string comparison work?

Tags:

java

Ok, this is stupid, but wtf is going on?

I have a String variable in a servlet, which takes the value of a parameter and based on that value I make a test to do something, but the if is not working. What is the problem?

 String action = request.getParameter("action");
    System.out.println("Action: " + action);
// I put 2 ifs to be sure, but not even one is working
    if(action.equals("something"))
            {
                System.out.println("hey");            
            }
    if(action.trim() == "something")
            {
                System.out.println("hey");
            }

On the console, the System.out.println shows me that the value of action is "something"

Action: something
like image 640
cc. Avatar asked Oct 07 '09 10:10

cc.


People also ask

Why we Cannot use == to compare string?

You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.

Do you use == or .equals for string?

Unfortunately, it's easy to accidentally use == to compare strings, but it will not work reliably. Remember: use equals() to compare strings. There is a variant of equals() called equalsIgnoreCase() that compares two strings, ignoring uppercase/lowercase differences.

Do the comparison operators work on strings?

The comparison operators also work on strings. To see if two strings are equal you simply write a boolean expression using the equality operator.

How do you perform a comparison string?

There are three ways to compare String in Java: By Using equals() Method. By Using == Operator. By compareTo() Method.


2 Answers

Your second comparison is wrong. You should also use equals instead of ==, like this:

if (action.trim().equals("something"))

The == operator compares references of (String) objects and under normal circumstances equal strings don't automatically have the same reference, i.e. they are different objects. (Unless both are internalized, but normally you shouldn't consider it)

Other than that your example works fine and the first comparison is valid. Try fixing the second comparison. If it works, you found your problem. If not, try using a debugger and double-check everything.

PS: When comparing literal strings with dynamic string objects, it's good practice to call the equals method on the literal string:

"something".equals(action)

That way you can avoid NullPointerExceptions when the string object is null.

like image 59
Daniel Rikowski Avatar answered Oct 26 '22 23:10

Daniel Rikowski


Your second condition is unlikely ever to be true - you're testing whether the string object created by trimming action is the same object as the string literal "something". This will only be true if action is set to that same literal value elsewhere. Use "something".equals( action.trim() ) instead.

Your first condition would be true the characters in the action string are the characters "something". If it's not true, then it doesn't. Assert it in a test, log it, print it or look at it in a debugger.

If you print a string for debugging, use something like System.out.println ( "String = >" + string + "<" ); so that it's obvious if there are any trailing spaces.

like image 25
Pete Kirkham Avatar answered Oct 26 '22 22:10

Pete Kirkham