Possible Duplicate:
How do I compare strings in Java?
I am trying to compare a striong using || operator..
if(publisher != "Niho books" || publisher != "Diamond Comics" )
what is wrong in this ? Strings can't be compared like this ??
Possible Duplicate:
How do I compare strings in Java?
I am trying to compare a striong using || operator..
if(publisher != "Niho books" || publisher != "Diamond Comics" )
what is wrong in this ? Strings can't be compared like this ??
This should work:
if (!"Niho books".equals(publisher) || !"Diamond Comics".equals(publisher))
http://www.leepoint.net/notes-java/data/strings/12stringcomparison.html
==
, !=
for String compareequals()
can go after publisher or after your string (publisher.equals()
or "string".equals()
) , I think the second one is maybe better because if your publisher is a null
you wont get nullpointer exception. But do however you like/is right for youIn this way you are not comparing contents... you are comparing references.
If you want to compare contents then use
publisher.equals("Niho books")
or
publisher.equalsIgnoreCase("Niho books")
Don't use !=
or ==
to compare strings in Java. Use .equals()
instead. The !=
and ==
operators compare references, not the content of objects.
if (!publisher.equals("Niho books") || !publisher.equals("Diamond Comics"))
In strings, equals() tests the equality of value where as != and == tests equality of the references.