Sitemap
Raj Dhok

Raj Dhok

I breath air.

3 min readDec 25, 2025

--

String, StringBuilder, StringBuffer in Java

Press enter or click to view image in full size

String: An immutable sequence of characters whose value cannot be changed once created.

StringBuilder: A mutable sequence of characters used for fast string modification in single threaded scenarios.

StringBuffer: A mutable sequence of characters that is synchronized and safe to use in multi threaded environments.

some textbook defination for you all.

In the previous blog we saw how String works and behaves, where it is stored in the memory.In this blog will be more focused on StringBuilder and StringBuffer.

Let’s start with String :

The comparesTo() method in String compare the String character by character and return ‘0’ if true ‘1’ if false (if they match of mismatch).

Example :

String s1 = “hello”;
String s2 = “hello”;
System.out.println(s1.compareTo(s2));

one by one h->e->l->l->o will get matched and the value inside them will get subtracted and the final answer will get returned.

What if they dont match?

Here is the answer :

String s1 = “hello”;
String s2 = “hallo”;
System.out.println(s1.compareTo(s2));//o/p : 4

ascii value for ‘e’ and ‘a’ are 101, 97 respectively.

step 1 will be h-h then e-a and as e-a is not zero it’ll return instantly and what it’ll return is the value after their subtraction i.e 4 in this case.

String s1 = “hello”;
String s2 = new String(“hello”);
System.out.println(s1.equals(s2));//true

this returns true as the .equals() method compare the actual content inside the objects not referance unlike ‘==’.

StringBuilder :

syntax — StringBuilder str = new StringBuilder();

str.append() : whatever you append will get appended to the string str itself no new string creation will be there unlike String.

Example :

Get Raj Dhok’s stories in your inbox

Join Medium for free to get updates from this writer.

StringBuilder str = new StringBuilder(“java”);

str.append(“script”);

System.out.println(str);//javascript.

you can append anything.

str.append(67.76);

str.append(false);//javascript67.76false

The defauld capacity of StringBuilder and StringBuffer is same i.e 16 bytes

StringBuilder str = new StringBuilder();

System.out.println(str.capacity());//16

but if you insert the string during the object creation like this :

StringBuilder str = new StringBuilder(“java”);

then str.capacity() will be : default + number of words added (in our case 16+4 = 20).

same goes for StringBuffer.

as in String .equals() method return true if the values inside them are same :

String s1 = “hello”;
String s2 = new String(“hello”);
System.out.println(s1.equals(s2));//true.

however in StringBuilder and Buffer :

StringBuffer sb1 = new StringBuffer(“hello”);
StringBuffer sb2 = new StringBuffer(“hello”);

System.out.println(sb1.equals(sb2)); // false
System.out.println(sb1 == sb2); // false

both ‘==’ and ‘.equals()’ returns false.

Why?

equals() method is actually part of object class and java team has coded the logic inside the equals() method inside the object class is such a way that it will compare references not the content, but how come in the String class it compare actual content, because inside the string class they override the equals() method and changed the body so that the actual content will get compare not reference.

the main difference between the StringBuilder and StringBuffer is one is synchronous, thread safe(StringBuffer) and the another is asynchronous, faster, not thread safe(StringBuilder).

StringBuffer is like an old man with multithreading support as its methods are synchronized, where StringBuilder came in jdk 1.5 (java 5) which is not thread safe i.e no multithreading support.

--

--