The string in Java is known as a “chain of characters”. The string object in Java is created using the “java.lang.String” class. This class gives multiple methods like size(), replace(), concat(), compareTo(), etc. to perform different operations. Once the object of the String class is created, any of these functions, such as the replace() function can be used to achieve a specific functionality.
The replace() method of the Java Strings will be elaborated in detail in this article.
The String replace() Method in Java
The replace() method of the Java String class replaces the specified character from the string or an entire substring with a new one. This method is suitable where the characters or substrings are repeated multiple times in the code and are to be replaced with a new one. An example will build a clear concept. The string “bun” needs to be converted into the string “run”, this is done just by replacing “b” with “r”.
Following are three different scenarios to understand the working of the replace() method of the String class.
- Replacing a character with another character
- Replacing the substring with a new substring
- Null Pointer Exception
Replacing a Character with a New Character
The replace() method of Java replaces the old character with the specified new character. Let us have a look at the syntax
public String replace(char oldCharacter, char newCharacter)
In the above Syntax
- The oldCharacter represents the character that needs to be replaced in the string.
- The newCharacter represents a character that will replace the old character.
The code below depicts the usage of the replace() method in Java.
public class ReplaceExample1{
//Java’s main() method
public static void main(String args[]){
//The String is passed
String s="The best programming language is Java";
System.out.println("The Original String is: " + s);
//The string to be replaced
String repstr=s.replace('a','c');
//Print the output
System.out.println("The String after replacement is: " + repstr);
}
}
In the above Java code:
- A class is declared as “ReplaceExample1”.
- The main() method of Java is declared.
- In the next step, the string is declared as “s”.
- The replace() method declared as “repstr” replaces the old character “a” with the new character “c”.
- The original String and the modified string are printed in the output.
Output
The below output shows that the occurrence of “a” is changed to “c” using the replace() method of Java.

Replacing a Substring with a New Substring
The replace() method in Java not only replaces a character but can also replace an old substring with a new one. Before proceeding with the code demonstration let us first have a look at the syntax depicted below
public String replace(char targetSubstring, char replacedSubstring)
- The “targetSubstring” is the substring that needs to be replaced.
- The “replacedSubstring” is the one that will replace the targeted substring.
The below Java code demonstrates the implementation of the replace() method.
public class ReplaceExample2{
//main() method of Java
public static void main(String args[]){
//The String is passed
String s="Java is easy to learn";
System.out.println("The Original String is: " + s);
//The string to be replaced
String repstr=s.replace("easy", "difficult");
//Print the output
System.out.println("The String after replacement is: " + repstr);
}
}
The above code is similar to the previous code except for a minor difference in the implementation of the replace() function.
- This time the replace function accepts two substrings “easy” and “difficult”.
- The target substring i.e., “easy” will be replaced with the “difficult” in the declared string.
- The original and modified strings are printed on the console to understand the working of the replace() function.
Output
The output below in Java verifies that the substring “easy” has been replaced with the new substring “difficult” using the replace() method.

Example 3: Declaring a Null Value to Replace with a New String
If the target value is declared as “null”, Java raises a NullPointerException, which means that the target is not specified. The code example below depicts the exception raised.
//The class in Javapublic class ReplaceExample3{
//Main class of Java
public static void main(String args[]){
//The String is passed
String s="The best programming language is Java";
System.out.println("The Original String is: " + s);
//The string to be replaced
String repstr=s.replace(null,"difficult");
//Print the output
System.out.println("The String after replacement is: " + repstr);
}
}
In the above Java code, when replacing the strings, if we pass null as the target value, a NullPointerException is raised.
Output
The output below shows the exception as “NullPointerException” since the target is “null”.

Conclusion
The replace() method of the Java String class is implemented in two different ways. One way is to replace the character present in the string and the other way is to replace the substring. In this article, we have implemented some examples that exhibit the behavior of the replace() method of the String class in Java.