public class TestStrings { public static void main (String[] argv) // Actually, an array of strings { String m = "I had tea in the Oval Office that Monday"; String b = "I do not recall having tea with her"; System.out.println ("Monica: " + m // Note concatenation + "\n" + // Expressions can spill over "Bill: " + b); // lines but strings may not. int i = b.length(); // length() method returns # chars in string System.out.println ("String \"" // Note backslash-quote + b + "\" has length " + i // Automatic conversion to string + " chars"); // Assignment: String b2 = b; // Actually, a pointer. But since strings are // immutable, there's no problem. System.out.println ("b2: " + b2); // Modifying strings using String methods: String b3 = b.substring (0,6); // First 7 letters // Must extract into a new string System.out.println ("b3: " + b3); char c1 = b.charAt (0); char c2 = b.charAt (2); System.out.println ("c1+c2: " + c1 + c2); if (b2.equals(b)) System.out.println ("b2 equals b"); // Short-cut operator: b2 += b2; // Concatenate a string with itself. System.out.println ("b2: " + b2); } }