/******************************************* * Program to demonstrate how Java handles * String objects are reused unless an * explicit call to "new" is made. ******************************************/ import java.io.*; public class TestString { public static void main(String[ ] args) { // creates a new String object s1 that contains "abc" String s1 = "abc", // points String object s2 to the above String s2 = s1, // points String object s3 to the above String s3 = "abc"; if (s1 == s2) System.out.println("s1 == s2"); if (s1 == s3) System.out.println("s1 == s3"); // forces Java to create a new String object s4 that contains "abc" String s4 = new String("abc"), // points s5 to the above String s5 = s4, // forces Java to create a new String object s6 that contains "abc" // this is not the same object that was created for s4 s6 = new String("abc"); if (s4 == s5) System.out.println("s4 == s5"); if (s4 == s6) System.out.println("s4 == s6"); if (s4.equals(s6)) System.out.println("s4 equals s6"); } }