Last Updated: January 3, 2026
When you think about programming, strings are among the most fundamental building blocks. They are used everywhere, from displaying messages to parsing user input.
In this chapter, we’ll dive deep into the basics of strings in Java.
At its core, a string is a sequence of characters. In Java, strings are represented by the String class, which is part of the java.lang package. It’s important to recognize that strings in Java are immutable, which means once a string is created, it cannot be changed. This immutability affects how you work with strings and can lead to both advantages and pitfalls.
You can declare a string in Java in a couple of straightforward ways. Here’s how:
The first method, using the string literal (enclosed in double quotes), is more common and preferred for its simplicity. The second method, using the new keyword, creates a new string object explicitly. While both methods work, they behave differently in terms of memory allocation and performance.
Prefer using string literals for better performance. The Java compiler optimizes string literals by storing them in a pool, which can save memory.
Now that we understand how to declare strings, let's look at some basic operations. Strings support various operations such as concatenation, length retrieval, and character access. Here’s how to perform these operations:
You can concatenate strings using the + operator or the concat() method.
To find the length of a string, use the length() method.
You can access individual characters in a string using the charAt(index) method, where the index starts at 0.
Understanding how to manipulate strings is crucial for many applications. For example, when processing user input in a web application, you may need to validate email addresses, format names, or even parse commands. Strings are the backbone of such functionalities.
Here’s a quick real-world example:
This snippet checks if the email string contains an '@' symbol, a simple but effective validation step.
One of the defining features of strings in Java is their immutability. This means that once you create a string, you cannot alter its content. What happens then if you try to modify a string?
Let’s look at an example:
In this case, concatenating " World!" doesn't change the original "Hello". Instead, a new string "Hello World!" is created, and original now points to this new string.
Immutability provides several benefits:
However, this characteristic can lead to inefficiencies when performing numerous modifications. That’s where StringBuilder and StringBuffer come into play (which we’ll cover in a later chapter).
Although Java does not support string interpolation in the same way as some other languages, it provides handy ways to achieve similar results. The most common method is using the String.format() function or the + operator, but there is also the newer String.join() and String.format() for more complex scenarios.
String.format():This method allows you to format strings with placeholders.
String.join():This method is useful for joining multiple strings with a delimiter.
Using these approaches can improve readability and maintainability, especially when constructing complex strings.
When comparing strings in Java, it's essential to understand the difference between == and the .equals() method.
== operator checks for reference equality, meaning it checks if both variables point to the same object in memory..equals() method checks for value equality, meaning it checks if the values of the strings are the same.Here’s a practical example:
In this snippet, str1 and str2 appear to have the same content but are stored in different locations, which is why == returns false.
Always use .equals() for string comparison unless you specifically need to check if two references point to the same object.
When working with strings, there are several edge cases to be aware of.
Attempting to call methods on a null string will result in a NullPointerException.
Always ensure that a string is non-null before performing operations.
An empty string ("") is different from a null string. An empty string is a valid string object with no characters, while null means that the string reference points to nothing.
Strings often come with leading or trailing whitespace, especially when handling user input. Use the trim() method to remove these spaces.
By being aware of these pitfalls, you can write more robust and error-free code.
Now that you understand the basics of strings, including their properties, initialization, and common operations, you are ready to explore string methods in depth.
In the next chapter, we will look at the various methods available in the String class that make string manipulation easier and more powerful.