Strings in Java

Nima Karimian Kakolaki
3 min readJul 8, 2024

--

In reality of programming, you can say your either working with numbers, Strings or both of them in combination. making it very important to have a good grip on the subject. Today we’re going to talk about Strings, StringBuilders and String Formatting.

Let’s start by having a simple “Hello World!” String.

String hello = "Hello World!";

Immutable Strings in Java

Strings in Java are Immutable, if you’re having some C++ background you’d be shocked about this fact. indeed, in C++ Strings are Arrays of character and you could change them in any index you’d like.

#include <iostream>
int main() {
char str[] = "hello";
str[0] = 'H'; // Modify the first character to 'H'
std::cout << str << std::endl; // Output: Hello
return 0;
}

But why did java decide to make Strings immutable? isn’t that inefficient? well java decided to accept this inefficiency for something more better performance wise.

https://unicminds.com/what-are-stack-and-heap-memory/

Java’s strategy for storing Strings is different than some languages such as c or C++. Java stores String objects in Heap in a String Pool. when you try to initialize a new String variable Java first looks in the String pool and if it has such string it just returns a pointer to that Object in Heap. for example if we have “Hello World!” in String Pool in Heap memory.

String firstVariable = "Hello World!";
String secondVariable = "Hello World!";

Both firstVariable and secondVariable point to the same String Object in String pool. if Strings were mutable, changing secondVariable would also change the firstVariable. Java sacrificed the efficiency of mutable strings for much better strategy which is String pools.

Mutable Strings in java

In scenarios where you need to build up strings from other string objects, such as keystrokes or words from a file, It would be inefficient to use string concatenation. Every concatenation you do constructs a new String object in the Heap’s String pool and is time consuming and memory expensive.

For this scenarion, Java has provided us with the StringBuilder class.

StringBuilder builder = new StringBuilder();
//Each time you need to add another part, call the append method.
String firstName = "Nima";
String lastName = "Karimian";
builder.append(firstName);
builder.append("\t");
builder.append(lastName);

Whenever you want to append a part to your String just use the append() method. finally when you’re done appending bits and parts to your string you can use the toString() method to convert your StringBuilder object to a Simple String object.

String completedString = builder.toString();

The StringBuffer class is less efficient than StringBuilder, but it allows multiple threads to add or remove characters. If all string editing happens in a single thread (which is usually the case), you should use StringBuilder. The APIs of both classes are identical. — core java Cay S.Horstmanm

Make sure to check my articles about Java — Spring for more interesting facts and knowledges.

Related articles:

--

--

Nima Karimian Kakolaki

Software engineer student with focus on information systems and machine learning.