/*****************************************************************
 *Written by Ben Mathew
 *for Cornell University CS 100 Summer 2001
 *
 *This class explores the equality of Strings using two different
 *types of equality comparisons. The fundamental point for this
 *class is to show the importance of using String.equals() for
 *String comparison, and not the == operator, because Strings are
 *not primitive datatypes.
 ****************************************************************/

public class StringEquality
{
    public static void main(String args[])
    {
        
        //str1 is a String that is constructed using an "anonymous" String
        String str1 = "I am a String";
        //str2 is also constructed with an anonymous String
        String str2 = "I am a String";
        //str3 is constructed with a String method known as a constructor,
        //but notice that it still uses str1
        String str3 = new String(str1);
        //str4 is constructed by an assignment statement
        String str4 = str3;
        

        //Print out the contents of each String
        System.out.println("str1 contains: " + str1);
        System.out.println("str2 contains: " + str2);
        System.out.println("str3 contains: " + str3);
        System.out.println("str4 contains: " + str4);

        System.out.println();

        //Test for equality between the strings using the == operator
        System.out.println(str1 == str2); //true
        System.out.println(str1 == str3); //false
        System.out.println(str1 == str4); //false
        System.out.println(str2 == str3); //false
        System.out.println(str2 == str4); //false
        System.out.println(str3 == str4); //true
        /* Why are only the first and last ones true? All the strings are the
         * same!! This is because of how Java appropriates String objects and
         * what this has to do with the == operator. I won't go into details
         * now, but str1 and str2 are made from the same "anonymous" String,
         * therefore, they both are considered the same by Java. Likewise,
         * str4 is made by assigning its value from str3, hence Java also
         * thinks that these are the same. However, since str4 is made from a
         * different String than str1 and str2, Java does not consider them to
         * be the same thing. Likewise with str3.
         */

        System.out.println();

        //Now test for equality using the String.equals() command
        System.out.println(str1.equals(str2));
        System.out.println(str1.equals(str3));
        System.out.println(str1.equals(str4));
        System.out.println(str2.equals(str3));
        System.out.println(str2.equals(str4));
        System.out.println(str3.equals(str4));
        /* All of these are true!!! How come? String.equals does a character by
         * character comparison of the Strings. Therefore, since they all
         * contain the same value, they are considered equal by this comparison.
         * For the purposes of CS 100, we would recommend that you use this
         * operator and not == for testing String equality.
         *
         * - b
         */
    }

}
