public static void main(String[] args) { String s1 = "test"; String s2 = "test"; if (s1 == s2) System.out.println("s1 == s2"); else System.out.println("s1 != s2"); }
結果:html
s1 == s2
public static void main(String[] args) { String s1 = "test"; String s2 = new String("test"); if (s1 == s2) System.out.println("s1 == s2"); else System.out.println("s1 != s2"); if(s1.equals(s2)) System.out.println("s1 equals s2"); else System.out.println("s1 is not equal to s2"); }
結果:ios
s1 != s2
s1 equals s2
public static void main(String[] args) { String s1 = "test"; String s2 = new String("test"); s2 = s2.intern(); if (s1 == s2) System.out.println("s1 == s2"); else System.out.println("s1 != s2"); if(s1.equals(s2)) System.out.println("s1 equals s2"); else System.out.println("s1 is not equal to s2"); }
結果:spa
s1 == s2
s1 equals s2