HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / String / Java String equals() method – Java compare strings

Java String equals() method – Java compare strings

Java String equals() method is used to compare a string with the method argument object.

1. Java String.equals() method

/**
* @param  anObject - The object to compare
* @return true -  if the non-null argument object represents the same sequence of characters to this string
*         false - in all other cases       
*/
public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = value.length;
        if (n == anotherString.value.length) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = 0;
            while (n-- != 0) {
                if (v1[i] != v2[i])
                    return false;
                i++;
            }
            return true;
        }
    }
    return false;
}
  1. String class overrides equals() method from Object class. The equality is done in case-sensitive manner.
  2. Use equals() method to check the equality of string contents.
  3. Do not use '==' operator. It checks the object references, which is not not desirable in most cases.
  4. Passing ‘null’ to method is allowed. It will return false.
  5. Use equalsIgnoreCase() method to check equal strings in case-insensitive manner.

2. Java String equals() method example

Java program to check if two strings are equal (case-sensitive).

public class Main 
{
    public static void main(String[] args) 
    {
        String blogName = "howtodoinjava.com";
        String authorName = "Lokesh gupta";
        
        //1 - check two strings for same character sequence
        boolean isEqualString = blogName.equals(authorName);    //false
        
        //2
        isEqualString = blogName.equals("howtodoinjava.com");   //true
        
        //3
        isEqualString = blogName.equals(null);                  //false
        
        //4 - case-sensitive
        isEqualString = blogName.equals("HOWTODOINJAVA.COM");   //false
    }
}

3. Java String equalsIgnoreCase() example

Java program to check if two strings are equal (case-insensitive). Notice that equals() and equalsIgnoreCase() methods behave in same way, except later is case-insensitive.

public static void main(String[] args) 
    {
        String blogName = "howtodoinjava.com";
        String authorName = "Lokesh gupta";
        
        //1 - check two strings for same character sequence
        boolean isEqualString = blogName.equalsIgnoreCase(authorName);    //false
        
        //2
        isEqualString = blogName.equalsIgnoreCase("howtodoinjava.com");   //true
        
        //3
        isEqualString = blogName.equalsIgnoreCase(null);                  //false
        
        //4 - case-insensitive
        isEqualString = blogName.equalsIgnoreCase("HOWTODOINJAVA.COM");   //TRUE
    }
}

4. Difference between == and equals in Java

As mentioned earlier, '==' operator checks for same object references. It does not check for string content.

Whereas equals() method checks for string content.

public class Main 
{
    public static void main(String[] args) 
    {
        String blogName1 = new String("howtodoinjava.com");
        String blogName2 = new String("howtodoinjava.com");
       
        boolean isEqual1 = blogName1.equals(blogName2);         //true
        boolean isEqual2 = blogName1 == blogName2;              //false
    }
}

Happy Learning !!

Reference:

Java String Doc

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

String methods

  • String concat()
  • String hashCode()
  • String contains()
  • String compareTo()
  • String compareToIgnoreCase()
  • String equals()
  • String equalsIgnoreCase()
  • String charAt()
  • String indexOf()
  • String lastIndexOf()
  • String intern()
  • String split()
  • String replace()
  • String replaceFirst()
  • String replaceAll()
  • String substring()
  • String startsWith()
  • String endsWith()
  • String toUpperCase()
  • String toLowerCase()

String examples

  • Convert String to int
  • Convert int to String
  • Convert String to long
  • Convert long to String
  • Convert CSV String to List
  • Java StackTrace to String
  • Convert float to String
  • String – Alignment
  • String – Immutable
  • String – StringJoiner
  • Java – Split string
  • String – Escape HTML
  • String – Unescape HTML
  • String – Convert to title case
  • String – Find duplicate words
  • String – Left pad a string
  • String – Right pad a string
  • String – Reverse recursively
  • String – Leading whitespaces
  • String – Trailing whitespaces
  • String – Remove whitespaces
  • String – Reverse words
  • String – Find duplicate characters
  • String – Check empty string
  • String – Get first 4 characters
  • String – Get last 4 characters
  • String – (123) 456-6789 pattern
  • String – Interview Questions

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)