Start course
Overview
Difficulty
Beginner
Duration
1h 31m
Students
28
Ratings
5/5
starstarstarstarstar
Description

In this course, we'll learn Strings in Java. 

Learning Objectives

  • What is String?
  • Creating String Objectives
  • Useful Methods of String Class
  • Immutable String
  • StringBuffer Class
  • StringBuilder Class

Intended Audience

  • 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

Prerequisites

  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge
Transcript

Hi there. In this video, we'll talk about the StringBuffer class. StringBuffer is the same as StringBuilder. It stores the object in HEAP and it can also be modified. StringBuffer has the same methods as the StringBuilder but each method in StringBuffer is synchronized i.e StringBuffer is thread-safe. The main difference between StringBuffer and StringBuilder is performance. StringBuilder is faster than StringBuffer because StringBuffer is synchronized. In our example, we define the strBuffer object with the Java value. This object is stored in HEAP memory and its value can be changed. 

And we use append method of the StringBuffer class and this method appends the value "Program" at the end. And it modifies value Java Program which is allowed in the StringBuffer. Let's make some examples with StringBuffer. In String project, right click on the source folder and select 'New class'. Specify package name as stringbuffer and class name as StringBufferExample and select the checkbox for the main method. In this exercise, we'll practice with StringBuffer methods to understand clearly. Let's create StringBuffer object that's named buffer 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 buffer. StringBuffer objects are mutable or changeable. So, after the append operation, object buffer points out "Welcome to Java". Let's try to learn some useful methods of StringBuffer by using them in the print method. In the first print method, we, print value of object buffer. 

Then, we use length method to find length of object buffer. The length method is the same as the stringLength method. Then, we use insert method to insert value "World" into a string of this StringBuffer by adding length method. The insert method adds the characters at a specified point. We used the length method of the buffer as the specified point. So, the value "World" will be added at the end of the buffer. Then, we use reverse method to reverse the value of StringBuffer object. String class in Java does not have a reverse method. However, StringBuffer and StringBuilder class have built-in the reverse method. Then, we use the reverse method again to get the original string. Finally, we use the delete method to remove the first 11 characters in a substring of this StringBuffer. Okay, let's run the code. You see the results in the console. The new value of the buffer object is "Welcome to Java". The length of the buffer object is 15. After the insert method, new value of buffer object is "Welcome to Java World". 

After the reverse method for the last value of the buffer object, the reversed string is displayed. We used reverse method again to get the original string. The original value of the buffer 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. As you can see, all methods of the StringBuilder class can also be used in the StringBuffer class without any other change. Okay, let's take a short break here. I'll see you in the next video.

 

About the Author
Students
458
Courses
47
Learning Paths
3

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.

Covered Topics