为什么产量存在差异

时间:2014-06-22 15:01:09

标签: java

我有两个程序,如下所示。

当我运行这两个程序时都工作正常。

但是当我比较时,第一个代码有两个字符串,结果为“ok”。

第二个代码也有两个字符串但这会给出结果“no”。为什么?

class Stringimp {

      public static void main(String[] args) {

      String str = "ali";
      String s1="ali";
      if (str == s1) {
        System.out.println("ok");
      } else {
      System.out.println("no");
    }
 }

}

 class Stringimp {

       public static void main(String[] args) {

       String str = "ali";
       String s1="ALI";
       s1=s1.toLowerCase();
       if (str == s1) {
           System.out.println("ok");
       } else {
           System.out.println("no");
       }
  }

}

1 个答案:

答案 0 :(得分:0)

因为您要比较引用,所以在第一个引用中,它们引用同一个对象,在

中 第二,他们没有。

相关问题