比较字符串的内容是否相同

必须使用equals()方法而不能用==。

例子:

public class Main {
    
    
    public static void main(String[] args) {
    
    
        String s1 = "hello";
        String s2 = "hello";
        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));
    }
}

从表面上看,两个字符串用==和equals()比较都为true,但实际上那只是Java编译器在编译期,会自动把所有相同的字符串当作一个对象放入常量池,自然s1和s2的引用就是相同的。

所以,这种==比较返回true纯属巧合。换一种写法,==比较就会失败:

public class Main {
public static void main(String[] args) {
String s1 = “hello”;
String s2 = “HELLO”.toLowerCase();
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
}
要忽略大小写比较,使用equalsIgnoreCase()方法。

猜你喜欢

转载自blog.csdn.net/Mr_zhang66/article/details/113242038