Strings are a special data type that’s built into the Java language but it’s not the same as primitive data types. Strings are a good example of a class. String is a data type. That means I can create variables of type “strings.” You’ll notice the difference – String starts with a capital S, not a lower case S like the other primitive data types. So, if I wanted to declare a variable called firstName, I’ll give it a value so it’s initialized. Let’s create another valuable, lastName – so this is my first and last name. – Let’s create a third variable. fullName and we’ll just leave it as that. We’ll go ahead and say fullName equals firstName plus a space in between them plus last name. We’ll print that out just so we can see what it actually has in it. So, it took firstName and concatenated the space on it and added lastName and that’s what we get.
Strings are a really useful data type that you’ll use a lot. What’s interesting about strings is because they’re classes, they have methods associated with them, so we can do a variety of interesting things with them. For instance, if I wanted to take the variable fullName and convert it to all lowercase, there’s a method for doing that called toLowerCase. So, by calling this method toLowerCase on the variable fullName, it will do that before it printed out. If you click “run code,” you can see it converted to lowercase.
There’s all sorts of other methods as you can imagine and you can find these methods in the documentation at docs.oracle.com. In this case, we’re looking at version 7, the standard edition of the java platform. Find the String class documentation and if you scroll down and look at the methods, you can see there’s all sorts of methods. For instance, if you want to know what’s the character at index three, then it will tell you what that is. So, if you could do that here for fullName, what’s the charAt index three? The interesting is because strings use array, essentially they’re like arrays. We start counting at zero so the D is index zero and the A is one, the V is two and the I is three. So, the character index of 3 should print out the character I. Run your code to check and see the results. The character at zero should be our capital D as you would expect.
What are some of the other methods that are available? We can go to the other direction, for instance. If I say, indexOf the character D is what index? This is going to find the first index and that’s at zero. What about the character V? That’s going to give us two, because V is the third index when you start with index 0. What about the N? Where’s that one going to be? It’s going to count all indexes plus the space and then continue on into here until it finds this, N. Remember, spaces are also characters and take up an index in a string array.
There are several other methods for the String class. For instance, if I wanted to replace a bunch characters I can use the “replace” method. If we look at the replace method in the documentation, we can replace one character with another character or we can replace an entire sequence of characters with another sequence of characters. So, let’s go and try that. Let’s say that I want to replace all of the Ns with Ws for some crazy reason and let’s run that and that’s going to give us back the string, the full name, David space Jansen with all of Ns replaced with Ws. So, the replace not only does its job but it returns the string. Now, actually, it doesn’t modify full name. Let’s go and check that. If we look at System.out.println and print out full name on the next line, we’ll see that fulName is still as it was at the beginning. So, replace doesn’t actually change the variable or the string of fullName. It just takes what’s in fulName, does the replacement and returns a new string with those modifications in it. Strings are a funny thing. They’re what they call “immutable,” meaning you can’t actually change a string. You can replace it with a different string but you cannot modify what’s in a string.
What are some of the others methods that are documented? For instance, if we did “startsWith,” let’s say, ‘startsWith Doctor.’ So, does the full name start with doctor and that’s going to tell us true or false and in this case, no, it doesn’t but if I would put “doctor” in the fullName variable at the beginning of it and run your code, it’s going to say true. You can see that it didn’t modify the full name.
Another thing we can do is we can just get subsequences or a part of the string. So, if I want a substring starting with index four, that’s going to return or give me– Let’s try that. There we go. So, asking for the substring starting at index four of fullName, that’s going skip zero, one, two, three and starting with index four. It’s starting with the A that’s going to give me the rest of the string from there on.
Similarly, we have subsequence what will give me a range. What if I wanted to go from four to seven? What’s that going to give me? That’s going to give me the A, V, I – four, five and six. So, it doesn’t include seven. It’s going up into the index of seven.
So, that’s just a variety of the different methods that are available with strings. You can see, there are a lot more. You can experiment with each them. Some of them use some concepts that you probably haven’t experimented yet. Get your fix of Java essentials today!