• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

What is the Difference Between “equals” and “contains” in Java?

July 30, 2023 //  by Talha Malik

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 typeBoolean Boolean 
Case sensitiveIt 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.

Category: Java

Previous Post: « What is the Cannot Find Symbol Java Error?
Next Post: What is the Purpose of “this” and “extends” in Java? »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Use Optional.ofNullable() Method in Java

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact