In Java, there is a char data type present that declares the variables of character type. But if we need to store the sequence of characters the strings are needed. The string in Java is an object that consists of separate characters and is used by the Java String class. There are numerous methods for the Strings in Java; one of them is the contains() method.
This write-up will effectively demonstrate the implementation of the contain() method of Java.
How to Use/Implement the String contains() Method in Java?
The string contains() method in Java searches for a sequence of characters from the specified string. It retrieves a boolean value: it returns “true” if the given sequence of characters is found in the specified string else it returns a “false”. The contain() method in Java can also be implemented using the if-else statement.
Let us have a look at the syntax of the contains() method.
public boolean contains(CharSequence sequence)
The next part of the article describes the implementation of the contains() method in Java.
Example 1: Basic Implementation of contains() Method of Java
The below code depicts the working of the contain() method in Java.
class StringContain{
//Main class of Java
public static void main(String args[])
{
String str1 = "Basic of Java Programming";
//Check if the string is present
System.out.println(str1.contains("Java"));
System.out.println(str1.contains("Code"));
//It returns false since case-sensitive in Java
System.out.println(str1.contains("programming"));
}
}
In the above block of code:
- In the first step, a class is declared as “StringContain”.
- A string is declared as “str1”.
- The next step involves the contains() method that checks for the specified string from the sequence of strings declared in str1.
- Since Java is case-sensitive when “programming” is declared it returns false since “p” is in lower case.
Output
The output below shows that the first output is returned true since “Java” is present in str1 whereas the two others are false since the strings “Code is not available” and “programming” doesn’t match the letter case convention with the original string “str1”.

Example 2: if-else Statement to Implement the String contains() Method
The code below implements the if-else statement to use the contains() method in Java.
//Declare a class for String contains() method
public class StringContain{
//Main class of Java
public static void main(String[] args) {
//Declare a string
String str1 = "Java Programming needs practice"; //if-else statement to check whether the string is present or not.
if(str1.contains("Programming")) {
System.out.println("This string contains Programming");
}else
System.out.println("The string is not found");
}
}
In the above code block:
- A public constructor is used to declare a class as “StringContains”.
- In the next step, a string is displayed as “str1”.
- In the if statement a string is declared as “Programming” with the contains() method to check for the availability in the string “str1”.
- The else statement returns the output when the string is absent in str1.
Output
The below output describes that the character sequence “Programming” is present in the main string str1.

Example 3: Null Pointer Exception
If a null value is passed to the contains() function, an exception occurs in Java.
//Declare a class for String contains() method
public class StringContain{
//Main class of Java
public static void main(String[] args) {
//Declare a string
String str1 = "Java Programming needs practice"; //if-else statement to check whether the string is present or not.
if(str1.contains(null)) {
System.out.println("The value is not null");
}else
System.out.println("The value is null");
}
}
In the above code block, a null value is declared in the if statement which throws an Exception that is a Null Pointer Exception in Java.
Output
The below output depicts that Java throws a “java.lang.NullPointerException” exception, indicating that a null parameter is passed to the “contains()” function.

The article has effectively demonstrated the implementation of the string contains() method of Java.
Conclusion
The string contains() method of Java searches for a sequence of characters from the declared string are retrieves a Boolean true or false depending upon whether the match is found. In this article, we have implemented the string contains() method of Java.