Java String equals()

Filed Under: Java

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

Java String equals()

Java String equals method

  • Java String equals() method overrides the Object class equals() method implementation.
  • Since String is immutable, checking the equality of string to another object should be done using equals() method rather than == operator.
  • String equals() method always return boolean value, it doesn’t throw any exceptions.
  • The result of equals() method is true if and only if – the argument is not null, it’s a String object, represents same sequence of characters as this string.
  • Below is the code snippet showing implementation details of equals() method.
    
    public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String aString = (String)anObject;
            if (coder() == aString.coder()) {
                return isLatin1() ? StringLatin1.equals(value, aString.value)
                                  : StringUTF16.equals(value, aString.value);
            }
        }
        return false;
    }
    

    The method uses some of the internal classes and functions of String class, just have a look how it’s written in properly optimized way.

  • If you want case insensitive equality check, then you can use String equalsIgnoreCase() method. It’s signature is public boolean equalsIgnoreCase(String anotherString), note that here the argument is String object.

Java String equals() method example

Here is a short example of string equals() method.


package com.journaldev.string;

public class JavaStringEqualsExample {

	public static void main(String[] args) {
		String str1 = "abc";
		String str2 = "abc";

		boolean isEqual = str1.equals(str2);

		System.out.println(isEqual); //true
	}

}

Java String equalsIgnoreCase() method example

Here is a short code snippet showing how to use equalsIgnoreCase() method.


String s1 = "ABC";
String s2 = "abc";
String s3 = "abcd";
System.out.println(s1.equalsIgnoreCase(s2)); //true
System.out.println(s1.equalsIgnoreCase(s3)); //false

Java String equals() example with user input

Here is an example where two strings are compared using equals() and equalsIgnoreCase() methods. Both of these string are entered by user on command prompt and we are using Scanner class to read them.


package com.journaldev.string;

import java.util.Scanner;

public class JavaStringEqualsExample {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter First String:");
		String str1 = sc.nextLine();
		System.out.println("Enter Second String:");
		String str2 = sc.nextLine();
		sc.close();

		System.out.println("Both Strings are Equal? = " + str1.equals(str2));
		System.out.println("Both Strings are Case Insensitive Equal? = " + str1.equalsIgnoreCase(str2));

	}

}

Here is a sample output produced when above program is executed in Eclipse IDE.

Java String equals method example

That’s all for Java String equals() method usage and examples.

Reference: API Doc

Comments

  1. Ajay Thakur says:

    package corejava;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Scanner;

    public class Palindrome {

    public static void main(String[] args) {
    Scanner s=new Scanner(System.in);
    System.out.println(“Enter your string”);
    String str=s.nextLine();
    String stray[]=str.split(” “);
    String t1=””;
    String t2=””;
    String smallpalen=””;
    ArrayList palen=new ArrayList();
    for(int i=0;i<stray.length;i++)
    {
    t1=stray[i];
    t2="";
    for(int j=0;j<t1.length();j++)
    {
    t2=t1.charAt(j)+t2;
    }
    if(t1.equalsIgnoreCase(t2))
    {System.out.println(t2);
    palen.add(t2);
    }

    }
    if(palen.size()==0)
    System.out.println(" sorry no palindrome found in your string");
    else
    {
    smallpalen=palen.get(0).toString();
    for(int k=0;kpalen.get(k).toString().length()?palen.get(k).toString():smallpalen;
    }
    System.out.println(“smallest palindrome in your string is : “+smallpalen);

    }
    }
    }

  2. afnas says:

    this is very helpful for me

  3. Ajay Thakur says:

    package corejava;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Scanner;

    public class Palindrome {

    public static void main(String[] args) {
    Scanner s=new Scanner(System.in);
    System.out.println(“Enter your string”);
    String str=s.nextLine();
    String stray[]=str.split(” “);
    String t1=””;
    String t2=””;
    String smallpalen=””;
    ArrayList palen=new ArrayList();
    for(int i=0;i<stray.length;i++)
    {
    t1=stray[i];
    t2="";
    for(int j=0;j<t1.length();j++)
    {
    t2=t1.charAt(j)+t2;
    }
    if(t1.equalsIgnoreCase(t2))
    {System.out.println(t2);
    palen.add(t2);
    }

    }
    if(palen.size()==0)
    System.out.println(" sorry no palindrome found in your string");
    else
    {
    smallpalen=palen.get(0).toString();
    for(int k=0;kpalen.get(k).toString().length()?palen.get(k).toString():smallpalen;
    }
    System.out.println(“smallest palindrome in your string is : “+smallpalen);

    }
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages