Java String equals()方法将此字符串与指定的对象进行比较。当且仅当参数不为null
并且是表示与此对象相同的字符序列的String
对象时,结果才为true
。
语法
以下是此方法的语法 -
public boolean equals(Object anObject)
参数
anObject
- 要比较的对象。
返回值
- 如果与
String
参数值相等,则此方法返回true
; 否则返回false
。
示例
public class Test {
public static void main(String args[]) {
String Str1 = new String("This is really not immutable!!");
String Str2 = Str1;
String Str3 = new String("This is really not immutable!!");
boolean retVal;
retVal = Str1.equals( Str2 );
System.out.println("Returned Value = " + retVal );
retVal = Str1.equals( Str3 );
System.out.println("Returned Value = " + retVal );
}
}
执行上面示例代码,得到以下结果:
Returned Value = true
Returned Value = true
上一篇:
Java String类
下一篇:
Java快速入门