You have actually been using the String data type for quite some time and to a large degree you may think that it was a simple or primitive data type. This is not surprising. Consider the variable declarations in figure 12.1. Figure 12.1 int number; char letter; double gpa; boolean finished; String title; You see five declarations and each declaration starts with a data type followed by a variable identifier. It appears that all five of the declarations behave in the same way. You may note one peculiar difference; the String declaration is the only data type that starts with an upper-case letter. Now look at the value assignments for each one of the variables in figure 12.2. Figure 12.2 number = 2500; letter = 'A'; gpa = 3.785; finished = true; title = "Exposure Java"; The assignment statement seems to give secondary evidence that String belongs with the simple data types. By now you have seen that data structures behave quite differently from simple data types. Accessing any element in an array requires not only the array identifier, but also an index to access a specific array element. Access of any field in an object, which is an attribute of an object in Java, requires the object identifier and some accessing method. There appears lots of evidence that the String data type is a very convenient and lovely simple data type. It is true that a string stores multiple characters, but after all you have treated a string like a single unit. The true nature of strings has intentionally been hidden. During the early chapters you have benefited very nicely by treating and using strings as if they were no different from the other simple data types. Now that you have started to learn about data structures and you have been exposed to a fair amount of Object Oriented Programming, the truth can be told.
equal and it does not matter which goes first. Program Java1216.java in figure 12.21 uses the compareTo method in this fashion. The user will enter two String values. The compareTo method is used to compare them. We then check whether this int value returned is positive, negative, or 0, which lets us know which String alphabetically goes first. Figure 12.21 // Java1216.java // This program the <compareTo> method being used to alphabetize 2 strings. // Note: This will not work properly if the strings are not both in the same "case". public class Java1216 { public static void main (String args[]) { System.out.println("\nJava1216.java\n"); System.out.print("Enter first string ===>> "); String s1 = Expo.enterString(); second string ===>> "); String s2 = Expo.enterString(); System.out.println(); if (s1.compareTo(s2) < 0) System.out.println(s1 + " goes before " + s2); else if (s1.compareTo(s2) > 0) System.out.println(s2 + " goes before " + s1); else System.out.println("Both strings are equal"); System.out.println(); } } The last output of program Java1216.java may look a bit strange. It says “ZEBRA goes before apple”. Since when does ‘Z’ come before ‘a’? This problem was discussed back in Java1210.java. The solution to this problem is to use one of the very methods introduced in Java1210.java. Those methods are toUpperCase and toLowerCase. To preperly determine which string goes first, we need to convert all strings to all CAPITAL letters or all lowercase letters before you compare them. Program Java1217.java in figure 12.22 does this and now the output actually makes sense. Both String variables get converted to upper case. Notice that “apple” actually changes to “APPLE” in the output.