==到目前为止,我一直在我的程序中使用运算符来比较我的所有字符串.但是,我遇到了一个错误,将其中一个更改为了.equals(),并修复了该错误.
是==坏?什么时候应该不应该使用它?有什么不同?
可能重复:
如何比较Java中的字符串?
为什么第一次比较(s1 == s2)显示相等而第二次比较(s1 == s3)显示不相等....?
public class StringComparison
{
public static void main( String [] args)
{
String s1 = "Arsalan";
String s2 = "Arsalan";
String s3 = new String ("Arsalan");
if ( s1 == s2 )
System.out.println (" S1 and S2 Both are equal...");
else
System.out.println ("S1 and S2 not equal");
if ( s1 == s3 )
System.out.println (" S1 and S3 Both are equal...");
else
System.out.println (" S1 and S3 are not equal");
}
}
Run Code Online (Sandbox Code Playgroud)