Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

String equals() in Java

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Sun Microsystems initially published Java in 1995 as a programming language and computing platform. It has grown from humble origins to power a significant portion of today's digital world by offering a secure foundation on which many services and applications are built. Java continues to be used in the development of new, innovative goods and digital services for the future.

Despite the fact that most recent Java applications integrate the Java runtime and the application, many programs, and even some websites will not work unless you have desktop Java installed.Hence learning about Java opens many different career paths and valuable opportunities. To become proficient in Java, one must understand different inbuilt methods in Java, one of which is equals(). We will be discussing the same below, along with its syntax and examples. 

String equals() Method

The string equals() method in Java is used to compare whether two strings are equal or not. 

Syntax:

string.equals(String str)

 

Parameters:

The equals method takes in a single parameter, str, which is of data type string.

Return Value:

  • returns false if the strings are not equal
  • returns true if the strings are equal

Let’s understand more about the method using some examples.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Examples

Example 1:

class A {
    public static void main(String[] args) {
      String stringOne = "This is a string";
      String stringTwo = "This is a string";
      String stringThree = "This is another string";
      boolean areSame;
 
     
      areSame = stringOne.equals(stringTwo);
      System.out.println(areSame);  // true
 
     
      areSame = stringOne.equals(stringThree);
 
      System.out.println(areSame);  // false
 
     
      areSame = stringThree.equals(stringOne);
      System.out.println(areSame);  // false
    }
  }

 

Output:

true
false
false

 

Example 2:

class A {
    public static void main(String[] args) {
      String stringOne = "This is a string";
      String stringTwo = "This is another string";
      if (stringOne.equals(stringTwo)) {
        System.out.println("Equal");
      }
      else {
        System.out.println("Not Equal");
      }
    }
  }

 

Output:

Not Equal

Frequently Asked Questions

What is the string equals() method in Java?

The string equals() method in Java is basically used to compare whether two strings are equal or not. 

Does the string equals() method take into account case differences?

Yes, the string equals() method takes into account case differences. Uppercase and lowercase words and letters are considered to be different. 

Which method should be used if we want to compare two strings without case differences?

Use the Java String compareToIgnoreCase() method to compare two strings while disregarding case differences.

Conclusion

In this article, we have extensively discussed the string equals() method in the Java programming language. We hope that this blog has helped you enhance your knowledge regarding the string equals() method and if you would like to learn more about the Java programming language, check out this article on Basics of JavaYou can refer to our guided paths on the Coding Ninjas Studio platform to find out more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. To learn more about Data Structures and Algorithms, you can enroll in our course on DSA in Java. 

Recommended problems -

Happy Coding!

Topics covered
1.
Introduction
2.
String equals() Method
3.
Examples
4.
Frequently Asked Questions
4.1.
What is the string equals() method in Java?
4.2.
Does the string equals() method take into account case differences?
4.3.
Which method should be used if we want to compare two strings without case differences?
5.
Conclusion