/ *
== is a comparison target address value, the contents if you really need string comparison, two methods may be used:
public boolean equals (Object obj): parameter can be any object, only parameter is a string and the same content will give true; otherwise false.
Precautions:
- Any object can receive with Object.
- equals method has symmetry, i.e. a.equals (b) and b.equals (a) the same effect.
- If the two sides compare a constant to a variable, the constant string EDITORIAL recommended.
Recommended: "abc" .equals (str) is not recommended: str.equals ( "abc")
public boolean equalsIgnoreCase (String str): Ignore case, the content comparison.
* /
public class Demo01StringEquals {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
char[] charArray = {'H', 'e', 'l', 'l', 'o'};
String str3 = new String(charArray);
System.out.println(str1.equals(str2)); // true
System.out.println(str2.equals(str3)); // true
System.out.println(str3.equals("Hello")); // true
System.out.println("Hello".equals(str1)); // true
String str4 = "hello";
System.out.println(str1.equals(str4)); // false
System.out.println("=================");
String str5 = null;
System.out.println("abc".equals(str5)); // 推荐:false
// System.out.println(str5.equals("abc")); // 不推荐:报错,空指针异常NullPointerException
System.out.println("=================");
String strA = "Java";
String strB = "java";
System.out.println(strA.equals(strB)); // false,严格区分大小写
System.out.println(strA.equalsIgnoreCase(strB)); // true,忽略大小写
// 注意,只有英文字母区分大小写,其他都不区分大小写
System.out.println("abc一123".equalsIgnoreCase("abc壹123")); // false
}
}