String comparison with "==" is not reliable?
- From: www <www@xxxxxxxxxx>
- Date: Thu, 29 Nov 2007 13:42:52 -0500
Hi,
I always thought I can use "==" to compare if two Strings are equal(with same case too). I know there is a method .equals(Object o) or equalsIgnoreCase(String s). But I always thought "==" is good enough.
This morning, I found out that I was wrong.
I have two Strings, str1 and str2. They received values from some where else. When I print them out to screen, they look same.(I am aware of some unvisible thing, so I used trim()
//str1 and str2
if(str1.trim() == str2.trim())
{
System.out.println("equal");
}
else
{
System.out.println("Not equal");
}
Above code print out "Not equal". But the following code print out "equal".
if(str1.trim().equalsIgnoreCase(str2.trim()))
{
System.out.println("equal");
}
else
{
System.out.println("Not equal");
}
Can you give me some hint? Thank you.
.
- Follow-Ups:
- Re: String comparison with "==" is not reliable?
- From: Joshua Cranmer
- Re: String comparison with "==" is not reliable?
- From: Jason Cavett
- Re: String comparison with "==" is not reliable?
- Prev by Date: Re: Array initialisation
- Next by Date: String comparison with "==" is not reliable?
- Previous by thread: Open xml file from jsp custom tag
- Next by thread: Re: String comparison with "==" is not reliable?
- Index(es):
Relevant Pages
|