java == equal String - 2021-04-08

public class Program {
    public static void main(String[] args) {
        String st1 = new String("123");
        String st2 = "123";
        String st3 = "123";
        String st4 = "1"+"2"+"3";
        String st5 = String.valueOf(1)+"23";
        String st6 = "12";
        st6 = st6+"3";
        System.out.println("---------1,2----------");
        System.out.println(st1 == st2);
        System.out.println(st1.equals(st2));
        System.out.println("---------2,3----------");
        System.out.println(st2 == st3);
        System.out.println(st3.equals(st3));
        System.out.println("---------3,4----------");
        System.out.println(st3 == st4);
        System.out.println(st3.equals(st4));
        System.out.println("---------3,5----------");
        System.out.println(st3 == st5);
        System.out.println(st3.equals(st5));
        System.out.println("---------4,5----------");
        System.out.println(st4 == st5);
        System.out.println(st4.equals(st5));
        System.out.println("---------2,6----------");
        System.out.println(st2 == st6);
        System.out.println(st2.equals(st6));
    }
}

在openjdk-16下编译运行输出:


---------1,2----------
false
true
---------2,3----------
true
true
---------3,4----------
true
true
---------3,5----------
false
true
---------4,5----------
false
true
---------2,6----------
false
true

Process finished with exit code 0

你可能感兴趣的:(java == equal String - 2021-04-08)