My blog has moved!

You should be automatically redirected in 6 seconds. If not, visit
http://kavachai.com
and update your bookmarks.

Friday, May 22, 2009

Comparing strings in Java

My nearest project will be for Android platform and this week I was exploring the platform and Java since last five years I've been developing using Microsoft .Net.

Yesterday I needed to check if response status from web service is eqaul to "ok". I was influented by .Net experience and did it just like this:
if (responseStatus == "ok")
I didn't expect this will not work. After some digging in Java documentation I found out that by using "==" operator I compared addresses of strings instead of contents.

The right ways to compare strings in Java are:
if (string1.equals(string2))
if (string1.equalsIgnoreCase(string2))

No comments:

Post a Comment