🌐
GeeksforGeeks
geeksforgeeks.org β€Ί java β€Ί string-vs-stringbuilder-vs-stringbuffer-in-java
String vs StringBuilder vs StringBuffer in Java - GeeksforGeeks
April 23, 2026 - If a string can change and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous, so you have thread-safety. If you don't want thread-safety than you can also go with StringBuilder class as it is not synchronized. The table below demonstrates the difference between ...
🌐
Learning Saint
learningsaint.com β€Ί blog β€Ί java-string-vs-stringbuilder-differences
Java String vs StringBuilder: Key Differences Explained
However, if your application involves frequent string modifications, especially in loops or dynamic operations, StringBuilder is the better option because of its speed and memory efficiency. Understanding their differences helps you write optimized, high-performance Java code. In short, use String for stability and StringBuilder for performance. Q1. What is the main difference between String and StringBuilder?
Discussions

In Java, why use StringBuilder instead of Strings?
Did you read the JavaDoc for StringBuilder? More on reddit.com
🌐 r/learnprogramming
75
134
September 4, 2022
Stringbuilder vs. String - why use String builder
A String in java is immutable. Basically when you do + using String it just creates another string and stores it in memory. In loops that can quickly get out of control. Stringbuilder concatenation does not have that problem. Hope my explanation was good. More on reddit.com
🌐 r/learnjava
10
7
March 7, 2018
hibernate - In Java, when should I prefer String over StringBuilder vice versa? - Stack Overflow
StringBuilder a stream of mutable characters) Use String to represent text values. By definition Java provides pooling of string values and thus providing you some space optimization. Think of this in a scenario where your application is dealing with millions of text values during a file batch processing. So as an example. ... Also, + operator is overloaded in String to construct String from different ... More on stackoverflow.com
🌐 stackoverflow.com
Java .equals between String and StringBuilder - Stack Overflow
Because they both are Different objects. String object!= StringBuilder object. ... Because in String class equals method ovverided in such a way. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Medium
medium.com β€Ί @AlexanderObregon β€Ί understanding-string-vs-stringbuilder-in-java-50448cbbf253
Understanding String vs StringBuilder in Java
April 26, 2024 - The choice between String and StringBuilder in Java depends on your specific requirements. For simple and small-scale operations where thread safety is required, String is a good choice.
🌐
Medium
dip-mazumder.medium.com β€Ί stringbuilder-vs-string-in-java-a-guide-for-optimal-memory-usage-4a284d8243ea
StringBuilder vs String in Java: A Guide for Optimal Memory Usage | by πŸš€ Backend Scaling Playbook | Medium
October 19, 2024 - `StringBuilder` outperforms `String` in Java for performance optimization. `String` is immutable, creating new objects for modifications, consuming memory and slowing execution. In contrast, `StringBuilder` is mutable, making it efficient for ...
🌐
Techie Delight
techiedelight.com β€Ί home β€Ί java β€Ί difference between string and stringbuilder in java
Difference between String and StringBuilder in Java | Techie Delight
July 7, 2026 - This post will discuss the difference between string and StringBuilder (or StringBuffer) in Java.. A String is immutable in Java, while a `StringBuilder` is mutable in Java.
🌐
DigitalOcean
digitalocean.com β€Ί community β€Ί tutorials β€Ί string-vs-stringbuffer-vs-stringbuilder
String vs StringBuffer vs StringBuilder in Java | DigitalOcean
Compare String, StringBuffer, and StringBuilder in Java. Learn differences in mutability, thread-safety, performance, and when to use each with examples.
Find elsewhere
🌐
EDUCBA
educba.com β€Ί home β€Ί software development β€Ί software development tutorials β€Ί top differences tutorial β€Ί string vs stringbuilder
String vs StringBuilder | Best 8 Comparisons of String vs StringBuilder
March 18, 2023 - In this article, String vs StringBuilder represents the sequence of characters; the first difference between them is the String class is in the System namespace, whereas StringBuilder is in System.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Medium
medium.com β€Ί @salvipriya97 β€Ί string-vs-stringbuilder-vs-stringbuffer-which-one-to-choose-4308dbcc3022
String vs StringBuilder vs StringBuffer: Which one to choose | by Priya Salvi | Medium
October 4, 2023 - If you need to manipulate strings dynamically and thread safety is not a concern, StringBuilder is the preferred choice due to its efficiency. If you require thread safety, especially in a multi-threaded environment, use StringBuffer.
🌐
Brainly
brainly.in β€Ί computer science β€Ί secondary school
Difference between string and stringbuilder in java - Brainly.in
June 14, 2023 - `StringBuilder` is more efficient for frequent modifications but lacks some of the built-in string manipulation methods available in `String`. It's important to choose the appropriate class based on the specific requirements of your program.
Top answer
1 of 5
4

You should use String, because String objects are cached in an object pool and might deliver better performance when you don't change them.

A StringBuilder is only useful when you keep on concatenating String tokens, which shouldn't be the case in a well normalized database table.

The JVM does all sorts of optimizations and chances are that even if you use concatenation the JVM might rewrite that routine to a StringBuilder version.

2 of 5
2

A simple rule of thumb (String is a type that represents character strings. StringBuilder a stream of mutable characters)

Use String to represent text values. By definition Java provides pooling of string values and thus providing you some space optimization. Think of this in a scenario where your application is dealing with millions of text values during a file batch processing. So as an example.

  String str1 = "Test";
  String str2 = "Test";

Here, str1 == str2 ( same reference)

Also, + operator is overloaded in String to construct String from different types. This can be used when constructing small Strings ( internally it gets done using StringBuilder so no worries) - but not while looping.

Use StringBuilder(or old counterpart StringBuffer) only when you are constructing a target String using small pieces of different types - and especially inside a loop - this will help you to avoid placing unnecessary string fragments in the string pool.

   StringBuilder s1 = new StringBuilder("test");
   StringBuilder s2 = new StringBuilder("test");

Here, s1 != s2

Also, I do not think there is someway you can manipulate the encoding of StringBuilder/Buffer - Meanwhile String allows this.

Edit: Representing Hibernate entities : Always use String to represent a text type in your class. For reasons stated above.This should come to you like muscle memory. For example, int, float, char etc for primitive types and String for text type. Use the builder only to build strings and not to represent a type unless that is some strange requirement.

🌐
DEV Community
dev.to β€Ί eronalves1996 β€Ί string-vs-stringbuffer-vs-stringbuilder-nbe
String vs StringBuffer vs StringBuilder - DEV Community
April 8, 2023 - So, we can append or insert some string or value inside these classes to craft a String and reduce overhead. ... The StringBuffer is suited for secure multi-threaded access. The StringBuilder is suited for performant single-threaded access.
🌐
Java Guides
javaguides.net β€Ί 2023 β€Ί 08 β€Ί string-vs-stringbuilder-vs-stringbuffer.html
String vs StringBuilder vs StringBuffer in Java
June 2, 2025 - String: Use when the text won't change, and thread safety is required. StringBuilder: Use in a single-threaded environment for frequent changes.
🌐
Sololearn
sololearn.com β€Ί en β€Ί Discuss β€Ί 58414 β€Ί what-is-the-difference-between-string-and-string-builder-with-examples-
what is the difference between string and string builder with examples? | Sololearn: Learn to code for FREE!
StringBuilder is preferable IF you are doing multiple loops, or forks in your code pass... however, for PURE performance, if you can get away with a SINGLE string declaration, then that is much more performant.
Top answer
1 of 12
9

Because they both are Different objects.

String object!= StringBuilder object.

But,Your doubt is

name1.equals(s)  returning true

Because in String class equals method ovverided in such a way.

And to get the desired output convert your StringBuilder to String.

System.out.println(s.equals(sb.toString())); //return true.

If you see the Source code of String#equals()

1012    public boolean  equals(Object anObject) {
1013        if (this == anObject) {
1014            return true;
1015        }
1016        if (anObject instanceof String) {             //key line 
1017            String anotherString = (String)anObject;
1018            int n = count;
1019            if (n == anotherString.count) {
1020                char v1[] = value;
1021                char v2[] = anotherString.value;
1022                int i = offset;
1023                int j = anotherString.offset;
1024                while (n-- != 0) {
1025                    if (v1[i++] != v2[j++])
1026                        return false;
1027                }
1028                return true;
1029            }
1030        }
1031        return false;
1032    }

The line if (anObject instanceof String) { always returns false incase if you pass StringBuilder.

2 of 12
8

The reason for the two false cases is that this is how the respective equals(Object) methods are specified to work.

  • For String.equals(Object), the javadoc says this:

    "Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object."

  • For StringBuilder.equals(Object), the equals method is inherited from Object, where the javadoc says this:

    "returns true if this object is the same as the obj argument; false otherwise."

So on Line #1 and Line #3, String.equals(Object) returns false because sb is not a String.

And if you reversed it and called sb.equals(name1) or sb.equals(s) you would get false also ... because sb.equals(...) is testing for the same object.


I dont understand why compiler does not think "name1" and "sb" as containing the same value

As you can see, it is nothing to do with the compiler ... and everything to do with the way that the equals method is specified.

🌐
Medium
medium.com β€Ί @cheetiharichandra1234 β€Ί string-vs-stringbuilder-in-java-which-one-should-you-choose-ef45f7aa79aa
String vs. StringBuilder in Java: Which One Should You Choose? | by Cheetiharichandra | Medium
December 27, 2024 - Each time you concatenate strings, a new `String` object is created, and the old one is discarded. β€” This creates overhead as Java needs to allocate memory and copy data repeatedly. 2. Mutable StringBuilder: β€” The `StringBuilder` class is mutable, meaning it modifies its internal character array without creating new objects.
🌐
Medium
medium.com β€Ί @mesfandiari77 β€Ί comparing-string-stringbuilder-and-stringbuffer-in-java-choosing-the-right-class-for-efficient-9f77cac4c2d8
Comparing String, StringBuilder, and StringBuffer in Java: Choosing the Right Class for Efficient String Manipulation | by MEsfandiari | Medium
August 4, 2023 - In conclusion, Java provides three classes for manipulating strings: String, StringBuilder, and StringBuffer. These classes have different characteristics, and using the appropriate class can significantly affect the performance of your program. If you need to manipulate a small string or you need thread-safety, use String.
🌐
Edureka
edureka.co β€Ί blog β€Ί string-vs-stringbuffer-vs-stringbuilder
String vs StringBuilder vs StringBuffer in Java | Edureka
March 29, 2022 - Strings are one of the most popular classes used while programming in Java by developers. But, since Strings are immutable, Java came up with two utility classes: StringBuilder and StringBuffer to make the String manipulations easy. So in this article, I will brief you on the differences between String, StringBuffer and StringBuilder.