Learn how to use strings in Java in this video. Kathryn introduces the toUpperCase(), toLowerCase(), contains(), length(), and charAt() methods as well as the char data type.
- [Instructor] One of the main data types you'll use in Java is the string data type. As we said before, a string is just a list of characters one after the other. Strings are immutable meaning you cannot change the content of a string after it's created. A single character can be represented with the data type char. Now let's say we wanted to create a program where the user would input a string and we would do a number of operations on that given string. Such as, make the letters uppercase or only print out a certain character within that string.
Since that data type string already exists, we won't need to create a new class for this program. Instead we'll do it in our already created main class. And so let's move over to the code and in here we are going to create a variable called user input and give it the value entertainment. So we'll go string, user input equals entertainment. That's going to be its value. In a future video we'll learn more about how to get input from the user through the scanner.
And so the user's actually going to be able to type into the console and the program will have access to that value and be able to manipulate it in various ways. That's coming up. Now if we do cmd + click on string, we'll be brought into the string data type in Java and be able to see how it works on a deeper level. In Java 8, strings are made up of chars in a single char array, but here in Java 9 they are made up of bytes in a byte array. And so here we see private final bytes.
And these bytes cannot be changed after initialization. Once you create a string and give it a value, that string itself cannot change. It's immutable. Now why did the creators of Java decide to put a string inside of a byte array in Java 9 versus a char array that they've used in previous versions? Well by having strings be composed of a byte array, this means that it takes less space in Java because a char itself takes up eight bytes. Now you can cmd + click into any of the words here that you see that might look a little confusing or that you want to have a deeper knowledge of.
All you have to do is cmd + click, and you can learn a lot more about what that specific data type or what that function does. Moving back to our main program, let's see what we can do with this user input string that we've just created. Because user input is in fact a string instance, we get a set of string instance methods for free that we can use on this variable. So if we go userInput. again another benefit of using an IDE we can see all the different instance methods that we can use right here.
We're going to use toUpperCase, and basically what this is going to do is it's going to output entertainment which is the value of user input, all in upper case. Also notice when I called this toUpperCase here, if I go back. Notice it has string here. This means that the output of toUpperCase is going to be a string which makes sense because entertainment all upper-cased is in fact still a string. Since it's returning the string output we are going to want to save it in a variable name here and that variable name we're going to call it uppercased.
Otherwise if we didn't save it we would lose the output of that function. To see what the output of the toUpperCase function looks like, we can go ahead and print it out here in the console, so we'll go to uppercased. And we'll go ahead and print that out. And here we have entertainment all in caps. Now did this change our initial user input? Well strings are immutable, so they cannot be changed after they are initialized. So if we go system.out.println, userInput, we'll see in fact that our initial input has not been changed by this toUpperCase function.
If we wanted to we could also get a specific character in a string. Let's say we wanted to access the first character. We could create a char variable, so we'll go char firstCharacter, so this'll be the first character of a given string. And in this case it's going to be our user input, so we'll do userInput. And again accessing those string instance methods we get for free, we're going to go charAt, and we're going to go index zero because our counting starts at zero with strings and so the first letter is at index zero, our second letter is at index one, and so forth to the end of the string.
Printing this out we'll be able to see what that first character is. And again this charAt method is not modifying user input in any way. It's only using user input as a data source to get that first character and then we print it out to the console here. And notice it's lower-cased, because the toUpperCase method did not modify the original user input. Now we don't always need to put the result in a variable, we can just print out the results.
Let's say we wanted to know if a certain string contained another string. We could just do a System.out.println, contains and then we'll do userInput.contains and so we're seeing if the user input contains this sub-string, enter. So does it contain enter? And we'll add that semicolon there. And basically what this means is that the .contains method will return true if user input contains enter, and false if it does not contain enter.
So if we go play here, we'll see in fact, entertainment does include enter. Now let's say we wanted to change enter which has a lowercase E, to one that has a capital E. Playing this we get false, because it has to be exact. And this is where kind of toUpperCase and you'll see in a minute toLowerCase, really help us in string modification it allows our input to be consolidated and normalized so we don't get errors like this. Because user input really does contain enter, we want it to contain enter.
So to fix this we could go .toLowerCase. And so here taking a hard-coded value, enter, and changing it to be lower-cased, and now this .contains method will return true. So if we press play here, we have true because we're saying does the user input, which is entertainment, contain enter when it's lower-cased. That's essentially what we're doing here and that is a fact that's true.
As you can see from above there are a lot of methods in Java that are self-explanatory and we don't necessarily need to know how exactly they work under the hood. We just need to know what the expected input and output are. However you'll notice that there's no way to change the actual content of a string. Strings are immutable and cannot be changed once they're created. The only way to change a string is to create a new one. In the next video we'll learn how to get user input through the scanner class which is a part of Java's Util package.
Released
3/21/2018- Downloading Java 9 and choosing an IDE
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Debugging
- Working with inheritance and interfaces
- Learning lambda
Share this video
Embed this video
Video: Strings