java程序中可以通过“==”和equals()两种方式对字符串进行比较,但是这两种方式是有明显区别的。
equals()方法用于比较两个字符串中的字符是否相等;
“==”用于比较两个字符串对象的地址是否相同;
例:
public class text01 {
public static void main(String[] args) {
String str1=new String("Hello world");
String str2=new String("Hello world");
System.out.println(str1.equals(str2));//结果为true
System.out.println(str1==str2);//结果为false
}
}