The course is part of this learning path
In this course, we'll learn Strings in Java.
- What is String?
- Creating String Objectives
- Useful Methods of String Class
- Immutable String
- StringBuffer Class
- StringBuilder Class
- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams
- No prior knowledge is required about the Java programming language
- Basic computer knowledge
Hi there. In this video, we'll talk about the StringBuilder Class. Java StringBuilder class is used to create mutable or modifiable string. At any point in time, it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls. It's the same as the string class, except it is mutable. So, the value of the StringBuilder object can be changed, which means it can be assigned the new value. In our example, we define the strBuilder object with the Java value. This object is stored in heap memory and its value can be changed. And we use the append method of the StringBuilder class, and this method appends the value program at the end, and it modifies the value Java program, which is allowed in the StringBuilder. Let's make some examples with StringBuilder.
In String project, right click on the source folder and select new class, specify package name as stringbuilder and class name as StringBuilderExample. And select the checkbox for the main method. In this exercise, we'll practice with StringBuilder methods to understand clearly. Let's create a StringBuilder object named builder with its value as "Welcome to". We use the append method to append value Java. The append method always adds these characters at the end of the builder object. StringBuilder objects are mutable or changeable. So, after the append operation, object builder points out "Welcome to Java." Let's try to learn some useful methods of StringBuilder by using them in the print method. In the first print method, we print the value of the object, builder. System.out.println(builder);
Then we use the length method to find the length of the object, builder. The length method is the same as the length method of the string class. System.out.println(builder.length()); Then we use the insert method to insert the value world into a string of this StringBuilder by adding the length method. System.out.println(builder.insert()); The first parameter will be builder.length, and the second parameter will be "World". Do not confuse the insert method with the replace method. Insert method adds the expression you specified in the second parameter to the index number you specified in the first parameter, and reinsert the element in that index from the end of this expression. So, it doesn't delete any element. The insert method adds the characters at a specified point. We use the length method of the builder as the specified point.
So, the value "World" will be added at the end of the builder. Okay, let's continue. Now I'll use the reverse method to reverse the value of the StringBuilder object. System.out.println(builder.reverse()); String class in Java does not have a reverse method. However, the StringBuilder class has the reverse method. So, it changes the index of numbers from last to first. So, after the reverse method, the first element of the builder object will be d, which is the last letter of the 'world' word and the last element of the builder object will be w, which is the first letter of the 'welcome'. Okay, then I will use the reverse method again to get the original string. So, I will copy and paste this line again. Okay, finally, I will use the delete method to remove the first 11 characters in a substring of this StringBuilder. System.out.println(builder.delete());
The first parameter will be zero and the second parameter will be 11. Okay, let's run the code. You see the results in the console. The new value of the builder object is "Welcome to Java." The length of the builder object is 15. After the insert method, the new value of the builder object is "Welcome to Java World". After the reverse model for the last value of the builder object, the phrase "Welcome to Java World" has been completely reversed and the reversed string is displayed. We used the reverse method again to get the original string. The original value of the builder object is displayed. After the delete method, the first 11 characters are removed from the original value of the string, and the last value "Java World" is displayed.
Before concluding the lesson, I'd like to point out an important difference between the string and StringBuilder classes. If you remember, we did a lot of examples about the isEqualTo and equals methods in our previous lessons, and we learned in detail, in which case it is possible to return true values and in which case, false values. Now I want to show you the difference in equality between the StringBuilder class and the string class. Let's create a new string first. String myString = new string("Hello"); And in the next line I write myString.concat(" Java"); And in the next line, I'll create another string. String myString2 = myString.concat(" World"); Okay, now let's print the myString and myString2 objects on the console. System.out.println(myString); System.out.println(myString2); Also I'll print the equality of these two strings.
System.out.println(myString == myString2); And System.out.println(myString.equals(myString2)); So, what do you think the result will be? If you want, let's examine the codes before testing. The first value of the myString object is "Hello". And on the next line we use the concat method of the string class and added the Java word to the myString object. But since the strings are immutable and since we don't assign the new value of the string to another string object, the value of the myString object is still "Hello" not "Hello Java". Please pay attention to this. And in the last line, we concatenated the myString object i.e. Hello Word with the world word and assigned it to a new string object, myString2 object. So, the myString2 object is Hello World. But the myString is still Hello. So, the output will be "Hello" and "Hello World". And these two objects are not the same.
So, the equality of these two strings will be false. Let's run and see. As you can see, "Hello" and "Hello World" are printed to the console and the equality is false. Okay, since the string class is not mutable, the result was like this. Well, now let's write the same codes with the StringBuilder class and observe the difference. I will create a StringBuilder object with the initial value, Hello. StringBuilder myBuilder = new stringBuilder("Hello"); And in the next line I write myBuilder.append(" Java"); And in the next line I will create another StringBuilder. StringBuilder myBuilder2 = myBuilder.append(" World"); Okay, now let's print the myBuilder and myBuilder2 objects on the console. System.out.println(myBuilder); System.out.println(myBuilder2); Also, I'll print the equality of these two StringBuilder objects. System.out.println(myBuilder == myBuilder2); and System.out.println(myBuilder.equals(myBuilder2)); So, what do you think the result will be?
If you want, let's examine the codes before testing. The first value of the myBuilder object is Hello. And on the next line, we used the append method of the StringBuilder class and added the Java word to the myBuilder object. Since the StringBuilder are mutable and the new value of the myBuilder object is "Hello Java" after this line. And in the last line, we concatenated the myBuilder object i.e. Hello word with the World word and assigned it to a new StringBuilder object, myBuilder2 object. So, the myBuilder2 object is "Hello Java World". And again the myBuilder object is also changed to the "Hello Java World" because the StringBuilder is mutable. So, the output will be "Hello Java World" for these two StringBuilder objects, and these two objects are the same.
So, the equality of these two strings will be true. Let's run and see. As you can see, this time, the "Hello Java World" is printed on the console and the equality of these two objects is true. This is the main difference between String class and StringBuilder class. Please pay attention to this. Yes, in this video, we did some examples to understand the StringBuilder class, its useful methods, and the differences between string class and StringBuilder class. StringBuilder class also has lots of methods. You can do more exercises with them. Let's take a short break here. See you in the next video.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.