AlgoMaster Logo

String Basics

Last Updated: January 3, 2026

6 min read

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.

Understanding 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.

Declaring and Initializing Strings

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.

Common Operations on Strings

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:

1. Concatenation:

You can concatenate strings using the + operator or the concat() method.

2. Length:

To find the length of a string, use the length() method.

3. Accessing Characters:

You can access individual characters in a string using the charAt(index) method, where the index starts at 0.

Practical Applications

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.

String Immutability

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.

Why Immutability Matters

Immutability provides several benefits:

  • Thread Safety: Since strings can't be altered, they can be safely shared between threads without synchronization issues.
  • Performance Optimization: The Java Virtual Machine (JVM) can optimize memory usage with immutable objects.
  • Hashing: Strings can be used as keys in a hash table without worrying about their values changing.

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).

String Interpolation

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.

1. Using String.format():

This method allows you to format strings with placeholders.

2. Using 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.

String Comparison

When comparing strings in Java, it's essential to understand the difference between == and the .equals() method.

  • The == operator checks for reference equality, meaning it checks if both variables point to the same object in memory.
  • The .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.

Edge Cases and Common Pitfalls

When working with strings, there are several edge cases to be aware of.

1. Null Strings:

Attempting to call methods on a null string will result in a NullPointerException.

Always ensure that a string is non-null before performing operations.

2. Empty vs. Null:

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.

3. Trimming Whitespace:

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.