Strings in Java

Understanding Strings in Java: A Beginner-Friendly Guide with Real Examples
If you're learning Java or preparing for an interview, one of the most important topics you’ll come across is Strings. Strings are used everywhere in Java programs—whether you’re collecting user input, displaying messages, or handling text data.
In this blog, we’ll walk through:
What is a String in Java?
How Strings behave behind the scenes
Useful things you can do with Strings
The common confusion between
==
and.equals()
Why Strings are immutable
Real examples and use cases
And a helpful link to Java interview questions on Strings
Let’s get started without overcomplicating anything.
What Is a String in Java?
In Java, a String is a group of characters. You can think of it as a sentence, a word, or even just a single letter—all of these are Strings.
Java treats Strings as a class, not just a data type. That means it has many built-in features to work with text easily. You create a String by putting your text inside double quotes, like this:
String greeting = "Welcome to Java!";
So now, greeting
holds that entire message. It may seem simple, but there's a lot happening in the background.
Strings Are Immutable (They Can’t Be Changed)
One thing that often surprises new learners is that Strings cannot be changed once they’re created. This is called immutability.
Let’s say you do this:
String name = "Kailas";
name = name + " Warade";
You might think that the original name
changed, but what really happens is this:
A new String is created by combining the two pieces.
The original "Kailas" still exists in memory.
Now,
name
is pointing to a brand-new String: "Kailas Warade".
This is important to understand because if your program uses a lot of string changes (like inside a loop), it might slow things down or use more memory. In those cases, Java gives you better tools like StringBuilder
.
Common Things You Can Do With Strings
Strings in Java come with many built-in methods that help you get things done quickly.
You can:
Find the length of a string using
length()
.Get a specific character using
charAt(index)
.Change everything to lowercase or uppercase using
toLowerCase()
ortoUpperCase()
.Remove spaces from the start and end with
trim()
.Compare two strings using
equals()
orequalsIgnoreCase()
.Cut a part of a string with
substring()
.Split the string into smaller parts using
split()
.
Each of these methods helps you work with real-world text data, whether it’s names, passwords, inputs, or file content.
\== vs .equals(): What’s the Real Difference?
This is a question many beginners ask. And it’s an important one for interviews too.
When you use ==
, Java checks if both strings point to the same place in memory. It doesn’t care about the content.
But when you use .equals()
, Java checks if the values inside the strings are the same.
Here’s a simple example:
String a = "Java";
String b = new String("Java");
System.out.println(a == b); // false
System.out.println(a.equals(b)); // true
Even though both strings have the same word—"Java"—the first check is false because b
is a new object. The second check is true because the content is the same.
How Java Handles Strings in Memory (The String Pool)
When you write a string like "Hello"
directly in your code, Java stores it in a special memory area called the String pool.
If another string with the same value is created later, Java will reuse the existing one instead of creating a new one. This saves memory and makes your program run faster.
However, when you use new String("Hello")
, it always creates a new object, even if "Hello"
already exists in the pool.
So, unless you have a specific reason, it’s better to use string literals (like "Hello"
) rather than new String()
.
Real-Life Example: Displaying Product Information
Let’s say you’re making an app that shows product details.
String product = "Smartphone";
double price = 49999.99;
String result = String.format("Product: %s | Price: ₹%.2f", product, price);
System.out.println(result);
This will print:
Product: Smartphone | Price: ₹49999.99
The String.format()
method is a great way to build clean, formatted strings for your output.
Interview Coming Up?
If you want to feel confident about Java interviews, don’t stop here.
We’ve prepared a full list of Java String interview questions along with simple answers and examples. It covers real-world concepts, differences between methods, memory handling, and common mistakes to avoid.
👉 Explore Java String Interview FAQs
This page is perfect for brushing up right before your interview.
📝 Final Thoughts
Strings are everywhere in Java—whether you’re building an app, handling form inputs, or printing reports. They seem simple, but they’re loaded with features and little tricks.
Here's a quick recap:
Strings are objects, not just text.
They are immutable, which means you can’t change them once they’re created.
Use
.equals()
to compare values, not==
.Java saves memory using the String pool.
Built-in methods like
substring()
,charAt()
, andsplit()
help you do more with less code.
Once you understand how Strings work and how to use them well, you’ll be more confident in your Java skills—whether you’re writing small programs or preparing for your dream job.
Subscribe to my newsletter
Read articles from Kailas Warade directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
