In Java language, different methods, and functions are used to create a program. These methods and functions are implemented in code according to the requirement like performing specific tasks and attaining particular outputs. For instance, for comparing the strings the “equals()” and “contains()” methods are used in Java.
This blog will describe the difference between “equals()” and “contains()” in Java.
What is “equals()” Method in Java
The “equals()” method is used to compare two strings and returns the output in boolean form, which means the output will be either “true” or “false”. We can also modify the output according to the requirements of our program. To get the output as “true” the refereed strings should be exactly equal. If we change the cases(upper/lower) of the characters, the output will be false because Java is a case-sensitive language.
Syntax
public boolean equals(string)
Parameters
The “string” in the above syntax represents the string that will be compared.
Return type
Its return type is boolean which means output is either “true” or “false”
What is “contains()” Method in Java
The “contains()” methods check whether the particular sequence of characters or string exists in the provided string or not. It’s not necessary to check the whole string in the “contains()” method unlike the “equals()” method. You can also check the particular sequence of characters as “contains()” checks each character of the string. The output of the “contains()” method also depends on the case(upper/lower) of characters. The “contains()” method always returns output in the form of boolean.
Syntax
public boolean contains(sequence)
Parameters
The “sequence” in the above syntax represents the sequence of characters that will be checked in the given string.
Return type
Its return type is boolean which means the output is either “true” or “false”
Difference Between “equals()” and “contains()” in Java?
Let’s check out the difference between equals() and contains() methods in the following table:
equals() | contains() | |
Input parameter | It takes two strings as input parameter and checks them. | It takes a sequence of characters as input. |
Return type | Boolean | Boolean |
Case sensitive | It is case sensitive which means changing of case will impact the output. | It is also case-sensitive. |
Let’s check out the following example to understand the difference between “equals()” and “contains() method:
class example {
public static void main(String[] args) {
String Str1 = "Java";
String Str2 = "Java";
String Str3 = "JAVA";
System.out.println("---------------Use of equals method:---------------");
System.out.println("String 1 is equal to String 2: "+Str1.equals(Str2));
System.out.println("String 1 is equal to String 3: "+Str1.equals(Str3));
System.out.println("---------------Use of contains method:---------------");
System.out.println("String 1 contains va:"+Str1.contains("va"));
System.out.println("String 1 contains java:"+Str1.contains("java"));
}
}
In this example,
- Firstly, we have initialized three strings “Str1”, “Str2” and “Str3” respectively.
- According to the above-given code, the first two strings are the same and the third one is in upper case.
- Then, call the “equals()” method inside the “System.out.println()” statement to compare the “Str1” string with the “Str2”. If both strings matched it will return “true” otherwise “false”.
- Next, again invokes the “equal()” method to compare the “Str1” string with the “Str3” respectively.
- After that, we used the “contains()” method to check whether the first string contains a sequence of characters like “va” and then “java” respectively.
The output of the above-described code is as follows:
That’s all! We have described the difference between “equals()” and “contains()” in Java.
Conclusion
In Java, the “equals()” method is utilized for comparing the two strings and checks whether they are the same or not. On the other hand, the “contains()” methods check if the provided string or particular sequence of characters exist in the given string or not. In this tutorial, we have provided the difference between “equals()” and “contains()” in Java with the aid of different examples.