Java String equals 示例

时间:2020-02-23 14:35:13  来源:igfitidea点击:

String的Equals方法将字符串与指定对象进行比较。
它检查两者是否具有相同的字符序列。
它通常用于比较两个字符串。

方法签名:

public boolean equals(Object anObject)

字符串等于示例:

package org.igi.theitroad;
 
public class StringEqualsExamplesMain {
 public static void main(String[] args) {
  String str1 = "Hello world from theitroad.com";
  String str2 = new String("Hello world from theitroad.com");
  String str3 = "Hello world from theitroad.COM";
 
  if (str1.equals(str2)) {
   System.out.println("1st and 2nd Strings are equal");
  } 
  else {
   System.out.println("1st and 2nd Strings are not equal");
  }
 
  if (str1.equals(str3)) {
   System.out.println("1st and 3rd Strings are equal");
  } 
  else {
   System.out.println("1st and 3rd Strings are not equal");
  }
 }
}

运行上面的程序时,我们将得到以下输出:

1st and 2nd Strings are equal
1st and 3rd Strings are not equal