Java String.equals() 方法与 == 方法。
equals() 方法将此字符串与指定的对象进行比较。仅当参数不为 null,且它是一个表示与该对象相同的字符序列的 String 对象时,才返回 true。
示例
public class Sample {
public static void main(String []args) {
String s1 = "tutorialspoint";
String s2 = "tutorialspoint";
String s3 = new String ("Tutorials Point");
System.out.println(s1.equals(s2));
System.out.println(s2.equals(s3));
}
}输出
true false
您也可以使用 == 运算符来比较两个字符串。但是,它比较给定变量的引用,而不是值。
示例
public class Sample {
public static void main(String []args) {
String s1 = "tutorialspoint";
String s2 = "tutorialspoint";
String s3 = new String ("Tutorials Point");
System.out.println(s1 == s2);
System.out.println(s2 == s3);
}
}输出
true false
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
JavaScript
PHP