Introduction
If you’re just starting with Java, you’ve probably come across the term String. It’s one of the most used classes in the language, yet many beginners find it a bit confusing at first. In this blog, you’ll find the Java String class explained in the simplest possible way—through real-life stories, easy code examples, and a friendly Q&A format. Whether you’re preparing for interviews or building your basics, this guide will make String handling in Java feel effortless.
What is the String class in Java?
The
Stringclass is declared inside thejava.langpackage.It is a non-primitive data type in Java.
It is used to store string (text) objects.
We can create String objects in two ways:
Without using
newoperatorWith using
newoperator
All String objects are stored in the String Pool Area inside Heap Memory.
The String Pool Area is further divided into:
Constant Pool Area
Non-Constant Pool Area
Without
newoperator → Stored in Constant Pool AreaWith
newoperator → Stored in Non-Constant Pool AreaConstant Pool Area doesn’t allow duplicates
Non-Constant Pool Area allows duplicates
How to Compare String Values in Java
Java provides three ways to compare String values:
1. Using == Operator
This is called the equality operator.
It compares the memory addresses (i.e., if both references point to the same object).
It does not compare the actual content inside the String.
Not a reliable method to compare non-primitive types like String.
2. Using .equals() Method
Defined in the Object class.
Compares the actual content inside the object.
It is case-sensitive (
"Java"≠"java").
3. Using .equalsIgnoreCase() Method
Defined in the String class.
Compares the content, but ignores case.
(i.e.,"Java"="java")
class StringDemo1 {
public static void main(String[] args) {
String s2 = "Java";
String s1 = "SQL";
String s3 = "SQL";
String str1 = new String("Java");
String str2 = new String("java");
String str3 = new String("SQL");
System.out.println(s1 == s3); // true (same reference)
System.out.println(s1 == str3); // false (different reference)
System.out.println(s1 == s2); // false
System.out.println(s1.equals(str3)); // true (same content)
System.out.println(str1 == str2); // false (different objects)
System.out.println(s2.equals(str1)); // true (same content)
System.out.println(s2.equalsIgnoreCase(str1)); // true (ignores case)
}
}
What is String Immutability in Java?
- In Java, the String class is immutable.
Immutable means: Once a String object is created, it cannot be changed.
If we try to change it (like adding more text), Java doesn’t modify the existing object. Instead, it creates a new one.
This happens internally—you still feel like you’re updating the same string, but Java is actually working behind the scenes.
The
Stringclass is immutable because it is declared as afinalclass—this means it cannot be extended or modified.

