equals( )
boolean equals(Object anObject) :将此字符串与指定的对象比较。
通俗的说就是:判断它俩(指定元素与括号中)内容是否相等!
equals不能用于基本数据类型的变量!
public static void main(String[] args) {
String str = "1111";
// true
System.out.println(str.contains("11"));
// true
System.out.println(str.contains(""));
// java.lang.NullPointerException
System.out.println(str.contains(null));
}
源码
/**
* Returns true if and only if this string contains the specified
* sequence of char values.
*
* @param s the sequence to search for
* @return true if this string contains <code>s</code>, false otherwise
* @throws NullPointerException if <code>s</code> is <code>null</code>
* @since 1.5
*/
public boolean contains(CharSequence s) {
return indexOf(s.toString()) > -1;
}