/********************************************************************* * Author: Eric Heim * Date Created: 5/31/2011 * Course: CS0007 * Description: Shows the basic functionality of various ways to * compare strings *********************************************************************/ public class StringComparison { public static void main(String[] args) { String name1 = "Mary", name2 = "mary", name3 = "Mark", name4 = "Mark"; int result; if(name3.equals(name4)) { System.out.println(name3 + " is the same as " + name4); } else { System.out.println(name3 + " is NOT the same as " + name4); } if(name1.equals(name3)) { System.out.println(name1 + " is the same as " + name3); } else { System.out.println(name1 + " is NOT the same as " + name3); } if(name1.equals(name2)) { System.out.println(name1 + " is the same as " + name2); } else { System.out.println(name1 + " is NOT the same as " + name2); } if(name1.equalsIgnoreCase(name2)) { System.out.println(name1 + " is the same as " + name2 + " when ignoring case"); } else { System.out.println(name1 + " is NOT the same as " + name2 + " when ignoring case"); } result = name1.compareTo(name3); System.out.println("The result of the comparison of " + name1 + " and " + name3 + " is " + result); result = name3.compareTo(name1); System.out.println("The result of the comparison of " + name3 + " and " + name1 + " is " + result); result = name3.compareTo(name3); System.out.println("The result of the comparison of " + name3 + " and " + name3 + " is " + result); result = name1.compareTo(name2); System.out.println("The result of the comparison of " + name1 + " and " + name2 + " is " + result); result = name1.compareToIgnoreCase(name2); System.out.println("The result of the comparison of " + name1 + " and " + name2 + " is " + result + " when " + "ignoring case"); } }