Mastering Strings in Java
Mastering Strings in Java
np
MASTERING STRING
IN JAVA
Take a closer look at Java’s String class —
how it works, why it’s immutable, and the
methods every dev should know.
🧠 How It Works
Whenever a string is created using double quotes,
STRING POOL
str
“Kiran”
str1
“Khanal”
str2
“Java”
str3
str4 “Kiran”
But,
🧪 Example
✅ Using .equals()
If you want to compare the content of two strings — whether
they hold the same sequence of characters — use the .equals()
method.
Even if the two strings are different objects, .equals() checks what's inside them —
the actual characters.
⚠️ Using ==
Here, a and b have the same content, but == returns false because a was created
using new, so it's a different object in the heap.
www.kiran-khanal.com.np
But don’t rely on this — always use .equals() for comparing string values.
🔤 .equalsIgnoreCase()
If you want to compare two strings without caring about case,
use .equalsIgnoreCase():
Kiran Khanal Software Developer
If you
find this
helpful, please
like and share
it with your
friends
www.linkedin.com/kirankhanalleo