简体   繁体   English

与数字连接时,Java字符串比较不起作用

[英]Java string comparison is not working when concatenated with number

My question to java folks is, when I am comparing two strings 我对java人的问题是,当我比较两个字符串时

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 虽然j和k的值是相同的,但它不起作用

Thanks in advance for your solution 在此先感谢您的解决方案

比较两个字符串时应该使用String.equals

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

You shouldn't compare strings with == , but instead use the .equals method: imageName[1].equals(imageName[2]) . 您不应该将字符串与==进行比较,而应使用.equals方法: 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. ==比较指针是否相等,所以如果两个变量都代表内存中完全相同的实例,那么它就是true In the first case, it's the case because Java pools String literals for performance. 在第一种情况下,情况就是这样,因为Java会将字符串文字用于性能。 But in your second case, you're getting two distinct heap-allocated objects, which, despite their content is identical, are two distinct objects nonetheless. 但在第二种情况下,您将获得两个不同的堆分配对象,尽管它们的内容相同,但仍然是两个不同的对象。

You are comparing whether the two String are exactly the same object. 您正在比较两个String是否完全相同的对象。

What you intended was to compare their contents. 你想要的是比较他们的内容。 I suggest you use .equals instead. 我建议你改用.equals

Never , ever, use "==" to compare Strings in Java. 永远不要使用“==”来比较Java中的字符串。 Use the equals() method. 使用equals()方法。 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. ==运算符检查两个String变量是否指向内存中的相同位置,而equals()方法检查两个单独的String对象是否包含相同的字符。 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". 这是第二个定义在这里有意义:你的String连接是创建单独的String对象,但我们仍然希望将它们视为“相等”。

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. 并且请在发布之前考虑在SO中进行研究,因为之前已经多次回答了这样的问题。

要比较两个String ,最好使用equals()方法


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

== 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 ie do those objects have the same contents (you'll appreciate there is a subtle difference here) 如果你使用equals()然后该方法比较字符串的内容,即这些对象具有相同的内容(你会理解这里有一个细微的差别)

Your first scenario works since the compiler is clever enough to realise that you have the same string twice. 你的第一个场景是有效的,因为编译器足够聪明,可以意识到你有两次相同的字符串。 ie 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 . 所以(一般来说)你应该使用equals()来比较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) 你可以使用引用相等(它更快,因为它比较引用而不是字符串内容),但你必须绝对确定你在做什么(也许你正在使用String.intern() - 但是有缺点那里)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM