String字符串的比较 Day15

package com.sxt.review;
/*
 * String字符串的比较
 * ==和equals()
 * 总结:比较String内容时用equals()方法
 */
public class TestEquals {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "hello";
        System.out.println(s1.equals(s2));
        //系统类String已经重写了equals()方法 比较的而是内容
        System.out.println("-------------"); //true
        //对于这样的类型的声明方式(不是new出来的)
        //串池把他们看做一个对象,所以池中只存储一分,只是有s1和s2 两个引用
        System.out.println(s1==s2); //true
        
        System.out.println("-------------");
        String s3 = new String("123");
        String s4 = new String("123");
        //系统类String已经重写了equals()方法 比较的而是内容
        System.out.println(s3.equals(s4));//true
        System.out.println("-------------");
        //new出来的对象在堆中 不同的地址
        System.out.println(s3==s4);//false
    }
}
原文地址:https://www.cnblogs.com/qingfengzhuimeng/p/6814558.html