Last Updated:

String Processing in Java

Strings in the Java language are very common. And everything is due to the fact that this is the very basis. And therefore, it is necessary to study this point. However, don't think it's complicated – after all, String, StringBuffer and StringBuilder are simple things. You just need to understand them. And for this one, just like that, there's this stuff.

After reading it to the end, the user will get the basics of string processing in Java, so that in the future it will be easier to study this issue, knowing about the simplest.

But before proceeding to StringBuffer and StringBuilder - you should pay attention to the simplest, met by users while reading the code. String – what is it? In fact, it will not be difficult to answer this question. This word denotes a class represented as a sequence of characters.

For example, "ThisisString" is an object of the String class. What readers also need to know from the initial is that instances of a class cannot be changed after they have been created (immutable), and there can be no legacy (final).

There are several examples of using String. For example, concatenation, or "addition" of lines of code, looks like this:

public static void main(String[] args) {
String command = «Follow» + » » + «the» + » » + «white» + » » + «rabbit»; System.out.println(command); Follow the white rabbit


If you dig deeper into the topic, you can understand that concatenation is performed using one of two classes, which, after all, are called StringBuffer and StringBuilder. Also, the Append method is used. It is necessary to understand what the 2 remaining classes are, which will be discussed later.

StringBuffer and StringBuilder – what are they?

As mentioned above, you should deal with the remaining classes. Both contain a sequence of characters. And both of them are used for the same purpose – they process strings. But they do have some features that a Java programmer should be aware of.

You need to understand that StringBuffer has the thread security provided by synchronization, and StringBuilder does not have any thread security at all. This is the disadvantage of the latter. But it is worth noting a similar feature - both classes are used when working with strings that are often modified. However, there is a difference even here. A StringBuilder is needed for a single-threaded environment, while a StringBuffer is needed for a multithreaded environment.

That both classes are exactly similar, and also distinguish them from the String described at the very beginning, both have mutability. That is, instead of "Immutable", users can observe the value of the "Mutable" parameter. And what String, StringBuffer and StringBulder have in common – all have no extensibility, since "Final" is a set value. This is, in general terms, a description of all three classes required to handle strings in Java.

More about StringBuffer


Having already understood the definition of StringBuffer and StringBuilder, it is worth talking in more detail about both classes. The first has several constructors for compiling part of the code. The difference between them is small:

  • StringBuffer() – required to create an "empty" inside the object. In such, as is already clear, there will be no symbols;
  • StringBuffer(str) - this constructor is already needed to ensure that something is inside (symbols).

In practice, you can use the options provided as you like. Here's what a user's design might look like:

StringBuffersb = new StringBuffer();
StringBuffer sb2 = new StringBuffer(«Not empty»);

Because the Append method is overloaded in a StringBuffer, the class can take in almost any data:

public static void main(String[] args) {
StringBuffer sb = new StringBuffer();

sb.append(new Integer(2));
sb.append(«; «);
sb.append(«; «);
sb.append(«; «);
System.out.println(sb); 2; false; [1, 2, 3];


To work with StringBuffer, you can also use methods such as:

  • Delete(int start, int end) – removes a line in the range from "start" to "end";
  • DeleteCharAt(intindex) – removes the symbol from the position from "index";
  • Insert(int offset, String str) – inserts data from "str" into "offset";
  • Replace(int start, int end, String str) – replaces characters from "start" to "end" using data from "str";
  • Reverse() – allows you to completely change the order of the symbols to the opposite (for example, how "Hello" changes to "Tevirp");
  • Substring(intstart) – returns a line where only the beginning of "start" is indicated. You can also add, as in other constructions, "intend" (in parentheses after "Substring").

More about StringBulder


StringBuilder is almost the same as StringBuffer. There are only some changes here. The most outstanding thing is the lack of synchronization. Because of this, the class will run much faster than StringBuilder. But, there is no flow protection at all.

Finally, it is worth showing how Stringbuilder is used in practice:

String numbers = «0123456789»;

StringBuildersb = new StringBuilder(numbers);
System.out.println(sb.substring(3)); 3456789
System.out.println(sb.substring(4, 8)); 4567
System.out.println(sb.replace(3, 5, «ABCDE»)); 012ABCDE56789
sb = new StringBuilder(numbers);
System.out.println(sb.reverse()); 9876543210
sb.reverse(); Let's return the original order

sb = new StringBuilder(numbers);
System.out.println(sb.delete(5, 9)); 012349
System.out.println(sb.deleteCharAt(1)); 02349
System.out.println(sb.insert(1, «One»)); 0One2349


Knowing all this, you can continue learning about Java in the future, delving into the topic of string processing or something else. After all, knowing how to use String, StringBuffer and StringBuilder is important for Java programming.