2

My question to java folks is, when I am comparing two strings

imageName=new String[20];    
....    
imageName[1]="img1";  
imageName[2]="img1";  

if(imageName[1]==imageName[2])  
{  
 ////  codes  
}

it works perfectly, but when I am making the string through number concatenation it's not working

imageName=new String[20];  
int j=1,k=1;  
imageName[1]="img"+j;  
imageName[2]="img"+k;

 if(imageName[1].toString()==imageName[2].toString())     
        {  
           ////  codes  
        }  

it's not working though the values of j and k are the same

Thanks in advance for your solution

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440

7 Answers7

7

You should use String.equals when comparing two Strings:

if (imageName[1].equals(imageName[2])
Dan W
  • 5,718
  • 4
  • 33
  • 44
  • Incidentally, "string".intern() == "string".intern() is valid :) – Chris Moran Aug 13 '12 at 14:50
  • 1
    @ChrisMoran It's valid because "string".equals("string") is true. From the javadoc, `It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true. ` – Dan W Aug 13 '12 at 14:53
1

You shouldn't compare strings with ==, but instead use the .equals method: imageName[1].equals(imageName[2]).

== compares the pointers for equality, so it'll be true if both variables represent the exact same instance in memory. In the first case, it's the case because Java pools String literals for performance. But in your second case, you're getting two distinct heap-allocated objects, which, despite their content is identical, are two distinct objects nonetheless.

Romain
  • 12,679
  • 3
  • 41
  • 54
1

You are comparing whether the two String are exactly the same object.

What you intended was to compare their contents. I suggest you use .equals instead.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

Never, ever, use "==" to compare Strings in Java. Use the equals() method. The == operator checks to see if two String variables are referring to the same location in memory, while the equals() method checks whether two separate String objects contain the same characters. It's this second definition that makes sense here: your String concatenation is creating separate String objects, but we still want to consider them as "equal".

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
1

The correct way to compare strings is using equals() method

So, Please change your code as below,

if (imageName[1].equals(imageName[2])

And please consider to do a research in SO before posting, as the questions like this have been answered many times before.

LGAP
  • 2,365
  • 17
  • 50
  • 71
0

To compare two Strings you better use equals() method

 if(imageName[1].equals(imageName[2]))
{
//// codes
}

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
touti
  • 1,164
  • 6
  • 18
0

== is a reference comparison. That is, you're determining if the two objects are, in fact, the same object. If you use equals() then that method comparses the contents of the string i.e. do those objects have the same contents (you'll appreciate there is a subtle difference here)

Your first scenario works since the compiler is clever enough to realise that you have the same string twice. i.e. it looks at:

imageName[1]="img1";  
imageName[2]="img1"; 

and determines that your array elements can point to the same object. In your second scenario, that's no longer true.

imageName[1]="img"+j;  
imageName[2]="img"+k;

The compiler can't reliably determine that these could be the same string object (quite correctly, too).

So (generally speaking) you should use equals() to compare Strings. You can use the reference equality (it's faster since it comparse the references rather than the string contents), but you have to be absolutely sure about what you're doing (perhaps you're using String.intern() - but there are disadvantages there)

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440