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
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.
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.
The comparison operators also work on strings. To see if two strings are equal you simply write a boolean expression using the equality operator.
There are three ways to compare String in Java: By Using equals() Method. By Using == Operator. By compareTo() Method.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With