What You Will Learn |
---|
|
In Java, strings are not primitive data; they are objects. Only primitive data can test relationships using the relational operators (==, !=, <, <=, >, >=). To test the relationship between objects requires the use of methods. For example, to test whether two strings are equal to each other, you must use the String class's equals method. Look at the following example.
String color = "red"; if(color.("red"))equals{ System.out.println("The color is red"); }// use equals method to test for string equality
The equals method will test each character of the two strings to see if they all match. It will return true if there is an exact match; otherwise it will return false. The equals method will also test the length of the two strings to see if they are the same. Strings of different lengths cannot be equal.
You cannot use the == operator to compare strings for equality. Look at the following example.
String color1 = "blue"; String color2 = "blue"; if(color1 == color2) { System.out.println("The colors are the same"); } else { System.out.println("The colors are different"); }
The output for the code prints what you would expect.
The colors are the same
However, if I modify the code like the following
String color1 = "blue"; String color2 =; if(color1 == color2) { System.out.println("The colors are the same"); } else { System.out.println("The colors are different"); }new String("blue")
The output for this code prints
The colors are different
even though they are the same string.
The reason for this discrepancy is the == operator does not test to see if two strings are equal; instead it tests to see whether the two string variables refer to the identical string object. You can have strings with identical contents stored in different objects, so you will never want to use this test in your programs.
In Java, letter case matters. For example, "John" and "JOHN" are not the same string. To ignore the letter case, use the equalsIgnoreCase method.
if(str1.equalsIgnoreCase(str2)) {}// body
If two strings are not identical to each other, you still may want to know the relationship between them. The compareTo method compares strings in dictionary order. If
str1.compareTo(str2) < 0
is true then the string str1 comes before the string str2 in the dictionary. For example, this is the case if str1 is "John", and str2 is "Steve". If
str1.compareTo(str2) > 0
is true then str1 comes after str2 in dictionary order. Finally, if
str1.compareTo(str2) == 0
is true then str1 and str2 are equal.
Actually, the "dictionary" ordering used by Java is slighly different from that of a normal dictionary. Java is case sensitive and sorts characters by putting numbers first, then uppercase characters, then lowercase characters. For example, 1 comes before B, which comes before a. The space character comes before all other characters.
Here is an example of code that uses compareTo method.
String s1 = "apple"; String s2 = "orange"; if(s1.compareTo(str2) < 0) { System.out.println(s1 + " is less than " + s2); } else { System.out.println(s2 + " is less than " + s1); }
The code would produce the following output.
apple is less than orange