Learn about strings and string operations in Java and the concept of mutability.
- [Narrator] Text represents such an important concept within information processing that most computer languages have designated data structures and methods for processing text. In Java the data structure is a string and the processing methods are those methods within the string class. You can find the Java doc for the string class at this URL. If you scroll down on this page you can see the methods that are available within the string class. The class strings test is available within the strings package underneath the test folder within your solutions module.
We begin with three constructors for string01, string02 and a character data class string03. First we're going to assert an equality between string01 and string02. Then we're going to use Java's double equal sign or equality operator and create a boolean that test whether string01 is equal to string02. But here we're going to assert false. In other words we're saying this flag is false but here we asserted that the equality is true.
One thing that you should realize about Java in general which is not restricted to the string class is the double equal sign represents a location operator. In other words the double equal sign is testing does string01 and string02 live in the same locations in memory within a Java virtual machine. On the other hand string01 and string02 are testing whether or not the contents of these fields are the same.
In these cases the string J-A-V-A is equal to the string J-A-V-A. In more formal terms the string class overrides the equals operator of the object class. And rather than returning the location based test it returns a content based test. We can test all these assertions by running this particular class. And we get all green so each of these assertions are correct.
The next thing that you probably want to learn about strings is that they're immutable within Java. Here we're looking at the situation we're creating a string called string01 and we're assigning it the value LinkedIn Learning. Now we're going to reassign string01 with the string LinkedIn Learning again but concatenate the string via Lynda.com. If we did an equality test on these we'd find that string01 changes even though we haven't redefined string01.
That is the concatenation of string changes the location in memory. The Java virtual machine copies the original information or the contents of the variable string01 into a new location and then pens the string via Lynda.com. If you'd like more information about memory locations you can navigate to this URL on stack overflow and it provides a good explanation which is beyond the scope of this class.
Finally once you recognize that Java strings are immutable you'd like to know about a class called string buffer. If you have a situation where you need to concatenate multiple strings the technique to do this is to use a string buffer. In this case we create a new string buffer, we append "is the time" to the original string "now". We use the string buffer method to string to create a new string and we assert "now is the time" is equal to the constructed string.
We can test this assertion by running the test and again we receive all green. Text and strings are such an important concept that you should spend the time to learn about their methods and their usages within Java.
- The IMQAV model
- Downloading software
- Installing and setting up a Java coding environment
- Mock tests
- Code coverage
- Using windows, views, and modes in IntelliJ IDEA
- Creating classes and attributes
- Creating constructors
- Casting variables
- Matching literals with regular expressions
- Regular expressions
- Design patterns