2014-10-22 69 views
-1

为什么第一个代码输出为“true”,第二个代码输出为“false”?比较Java中的字符串

code 1 ............... String str1 =“abc”; String str2 =“abc”;

if (str1==str2) 
     System.out.println("true"); 
    else 
     System.out.println("false"); 

代码2

注:输入到两个字符串将是相同的值 ....................

Scanner sc=new Scanner(System.in); 
System.out.println("enter string 1"); 
String String1=sc.next(); 
System.out.println("enter string 2"); 
String String2=sc.next(); 

if(String1==String2) 
    System.out.println("true"); 
else 
    System.out.println("false"); 
+4

这是* 5,423,232th *时间这已被要求 – TheLostMind 2014-10-22 04:48:46

+1

堆栈溢出应执行搜索... – mkobit 2014-10-22 04:50:08

+0

@MikeKobit true – Victor2748 2014-10-22 04:50:24

回答

0

==检查对象标识 的.equals()方法检查该值是否等于另一个字符串的值 使用操作员

+0

我的第一个代码是否等于'.equals()'? – Erandi 2014-10-22 04:57:12

+1

你是什么意思? – Adz 2014-10-22 08:52:42