Why use equals() to compare strings in Java
Hi All,
Many might aware of the reason to use equals() method in Java string comparison. Let me summarize the reason behind using it. I once tried to write a simple java program to compare both strings in java using double equals operator(==). I assigned value to the first variable as “Hello” and to the next variable i assigned the value as like “Hel” + “lo”. I then compared both the variable using double equals operator(==). As everyone expects, I too think that it goes through the equals condition. But it went to non-equal condition loop. I went on to search for the reason with google. Let me write the code snippet of it
The reason behind failing of String comparison using double equals operator is it looks for the reference value. Hence for string comparison you should use only equals() or equalsIgnoreCase()
Thanks for reading,
Regards,
Muthuselvan N
-
Archives
- March 2011 (1)
- June 2010 (3)
- May 2010 (1)
- January 2010 (3)
- December 2009 (1)
- August 2009 (1)
- June 2009 (1)
- April 2009 (4)
- November 2008 (1)
- October 2008 (3)
- March 2008 (1)
- September 2007 (1)
-
Categories
-
RSS
Entries RSS
Comments RSS