001    /**
002     * Exercise 3.6 from the book.
003     * 
004     * @author Kristian Torp, torp (at) cs (dot) aau (dot) dk
005     * @version 1.0
006     */
007    public class Exercise3_6 {
008            /**
009             * Compares two strings and prints how they successfully compare
010             * 
011             * @param s1
012             *            The first string
013             * @param s2
014             *            The second string
015             */
016            public static void cmp(String s1, String s2) {
017                    // are the two string ==
018                    if (s1 == s2) {
019                            System.out.println(s1 + " == " + s2);
020                    } else {
021                            System.out.println(s1 + " != " + s2);
022                    }
023    
024                    /*
025                     * This does not work if (s1 <= s2) { System.out.println (s1 + " != " +
026                     * s2); }
027                     */
028    
029                    // are the equals
030                    if (s1.equals(s2)) {
031                            System.out.println(s1 + " equals() " + s2);
032                    } else {
033                            System.out.println(s1 + " NOT equals() " + s2);
034    
035                    }
036    
037            }
038    
039            /**
040             * The main method.
041             */
042            public static void main(String[] args) {
043                    // create two strings and given them default values
044                    String str1 = "Viggo";
045                    String str2 = "Hansen";
046                    String str3 = "Hansen";
047                    String str4 = new String("Hansen");
048    
049                    // if user arguments then use these
050                    if (args.length >= 1) {
051                            str1 = args[0];
052                            str2 = args[1];
053                            cmp(str1, str2);
054    
055                    } else {
056                            cmp(str1, str2);
057                            cmp(str2, str3); // note this
058                            cmp(str2, str4); // note this
059                    }
060            }
061    }
062