Java replace() vs replaceAll() – The Complete Beginner's Guide
When learning Java, one of the most common doubts is:
“Both
replace()andreplaceAll()replace text. So why does Java have two methods?"
At first glance, they appear to do the same thing. In many programs, they even produce the same output.
However, there is one very important difference between them.
By the end of this article, you’ll clearly understand:
- What
replace()does - What
replaceAll()does - What Regular Expressions (Regex) are
- When to use each method
- Common interview questions
Understanding replace()
The replace() method replaces exact characters or strings.
It performs a literal replacement, meaning Java looks for exactly what you provide and replaces it.
It does not understand Regular Expressions (Regex).
Syntax
replace(char oldChar, char newChar)replace(CharSequence oldString, CharSequence newString)Example 1 — Replacing Characters
String str = "banana";System.out.println(str.replace('a', 'o'));Output
bononoHow it works
Original String
b a n a n aReplace every 'a' with 'o'
b o n o n oJava simply checks each character.
If the character is 'a', it replaces it.
Otherwise, it leaves it unchanged.
Example 2 — Replacing Words
String str = "Java is easy";System.out.println(str.replace("Java", "Python"));Output
Python is easyAgain, Java searches for the exact word “Java” and replaces it.
No special pattern matching happens.
Understanding replaceAll()
Unlike replace(), the replaceAll() method does not search for exact text.
Instead, it searches for patterns.
Those patterns are written using Regular Expressions, commonly called Regex.
Syntax
replaceAll(String regex, String replacement)Notice that the first parameter is called:
regexnot
textThat small difference changes everything.
What is Regex?
Regex stands for Regular Expression.
A Regular Expression is simply a search pattern.
Instead of saying:
Replace only the character
'1'
You can say:
Replace every digit.
Instead of saying:
Replace only spaces
You can say:
Replace every whitespace character.
Instead of saying:
Replace only uppercase
A
You can say:
Replace every uppercase letter.
Regex allows Java to recognize patterns instead of exact text.
Example — Replace Every Digit
String str = "Java123Programming45";System.out.println(str.replaceAll("\\d", "*"));Output
Java***Programming**Why?
Because
\dmeans
Any digit (0–9)
Java finds:
1
2
3
4
5and replaces every one of them with *.
Example — Replace Every Space
String str = "Java Programming Language";System.out.println(str.replaceAll("\\s", "_"));Output
Java_Programming_LanguageHere,
\smeans
Any whitespace character.
Every space becomes an underscore.
Example — Replace Every Uppercase Letter
String str = "Java Programming";System.out.println(str.replaceAll("[A-Z]", "*"));Output
*ava *rogrammingThe pattern
[A-Z]means
Any uppercase letter.
Therefore,
J
Pare replaced.
Why Beginners Think They Are the Same
Consider this code.
String str = "banana";System.out.println(str.replace("a", "o"));
System.out.println(str.replaceAll("a", "o"));Output
bonono
bononoBoth methods give exactly the same output.
Get Akash S’s stories in your inbox
Join Medium for free to get updates from this writer.
This is why beginners often think both methods are identical.
They are not.
The difference becomes visible only when Regular Expressions are used.
The Best Example to Understand the Difference
Let’s take this string.
String str = "a.b.c";Using replace()
System.out.println(str.replace(".", "*"));Output
a*b*cWhy?
Because replace() treats
.as a normal dot character.
Only the dots are replaced.
Using replaceAll()
System.out.println(str.replaceAll(".", "*"));Output
*****This surprises many Java learners.
Why did every character disappear?
Because in Regex,
.does not mean a dot.
It means
Any single character.
Java replaces:
a
.
b
.
cEvery character matches the pattern.
Therefore,
*****is printed.
Replacing an Actual Dot Using replaceAll()
If you really want to replace only the dot character, you must escape it.
System.out.println(str.replaceAll("\\.", "*"));Output
a*b*cHere,
\.means
A literal dot.
Common Regex Patterns
RegexMeaning\\dAny digit\\DAny non-digit\\sAny whitespace\\SAny non-whitespace.Any single character[A-Z]Uppercase letters[a-z]Lowercase letters[0-9]Digits[^0-9]Everything except digits
Side-by-Side Comparison
Consider this string.
String str = "Java123";Using replace()
System.out.println(str.replace("1", "*"));Output
Java*23Only the character '1' is replaced.
Using replaceAll()
System.out.println(str.replaceAll("\\d", "*"));Output
Java***All digits are replaced because \\d matches every digit.
Performance
For simple replacements,
replace()is usually faster because it directly replaces characters or strings.
replaceAll() first processes the Regular Expression, then performs the replacement.
If you don’t need Regex, always choose replace().
Quick Comparison Table
Featurereplace()replaceAll()Replaces exact text✅ Yes❌ No (uses regex patterns)Supports Regular Expressions❌ No✅ YesReplace digits (\\d)❌ No✅ YesReplace spaces (\\s)❌ No✅ YesReplace uppercase letters ([A-Z])❌ No✅ YesFaster✅ Yes❌ Slightly slowerBest used forExact characters or stringsPattern-based replacement
Interview Questions
1. Which method supports Regular Expressions?
Answer: replaceAll()
2. Which method is faster?
Answer: replace(), because it doesn't process Regex.
3. Which method should be used to replace an exact word?
Answer: replace()
Example:
str.replace("Java", "Python");4. Which method should be used to replace all digits or spaces?
Answer: replaceAll()
Example:
str.replaceAll("\\d", "*");
str.replaceAll("\\s", "_");Conclusion
Although replace() and replaceAll() seem similar, they are designed for different purposes.
- Use
replace()when you know the exact character or string you want to replace. - Use
replaceAll()when you want to replace text that matches a pattern, such as all digits, spaces, uppercase letters, or other Regex expressions.
A simple way to remember the difference is:
replace()→ Exact textreplaceAll()→ Pattern (Regex)









