/**

* Some important stuff about String:

 * See the Q & A below.

 

 * Book: M.A. Weiss. Data Structures & Problem Solving Using Java, 4th EDITION. Addison Wesley / Pearson. 2009. (ISBN: 0321541405)

 * p.39: "Since an array is a reference type, = does not copy arrays. Instead, if lhs and rhs are arrays, the effect of

               int [ ] lhs = new int [ 100 ];

               int [ ] rhs = new int [ 100 ];

               ...

               lhs = rhs;

  is that the array object that was referenced by rhs is now also referenced by lhs.

  Thus changing rhs[ 0] also changes lhs[ 0].

 (To make lhs an independent copy of rhs, one could use the clone method, but often making complete copies is not really needed.)"

 

 * Exercise: Run the sample program and verify the highlighted Q & A.

**/

 

public class StringTest {

               public static void main (String args[]) {

                              String st1 = "John Doe";

                              String st2 = new String();

                             

                              //Q: What does st1 == st2 return?

                              //A: p.33: "For reference types, == is true only if the two strings reference the same object."

                              System.out.println ("------------ References comparison: ");

                              System.out.println ("st1: " + st1 + "\tst2: " + st2);

                              if (st1 == st2)

                                             System.out.println ("'" + st1 + "'" + " references the same object as " + "'" + st2 + "'.");

                              else

                                             System.out.println ("'" + st1 + "' and '" + st2 + "' reference different objects.");

 

                              st2 = st1; //st2 and st1 reference the same string

                              System.out.println ("st1: " + st1 + "\tst2: " + st2);

                              if (st1 == st2)

                                             System.out.println ("'" + st1 + "'" + " references the same object as " + "'" + st2 + "'.");

                              else

                                             System.out.println ("'" + st1 + "' and '" + st2 + "' reference different objects.");

 

                              //Q: How can we make st3 an independent String but with the same value as st1?

                              //A: Use the existing string to initialize a new string

                              String st3 = new String(st1); //use the content of st1 to initialize a new String, st3

                                             //Note: st3 and st1 are two independent strings

                              System.out.println ("st1: " + st1 + "\tst3: " + st3);

                              if (st1 == st3)

                                             System.out.println ("'" + st1 + "'" + " references the same object as " + "'" + st3 + "'.");

                              else

                                             System.out.println ("'" + st1 + "' and '" + st3 + "' reference different objects.");

 

                              //Q: Then, how do you compare the 'content' of two strings?

                              //A: Use the equals() method

                              //p.34: "The equals( ) method can be used to test whether two references reference objects that have identical states."

                              System.out.println ("------------ Values comparison: ");

                              if (st1.equals(st3))

                                             System.out.println ("st1 has the same value as st3.");

                              else

                                             System.out.println ("st1 and st3 do not have the same value.");

 

                              //alternatively, the compareTo() method may be used for comparison

                              if (st1.compareTo(st3) == 0) //<0 if st1 < st3; >0 if st1 > st3

                                             System.out.println ("'" + st1 + "'" + " has the same value as " + "'" + st3 + "'.");

                              else if (st1.compareTo(st3) < 0)

                                             System.out.println ("'" + st1 + "' < '" + st3 + "'.");

                              else

                                             System.out.println ("'" + st1 + "' > '" + st3 + "'.");

 

               } // main

 

} // StringTest class