Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Gaurav Kukade
Gaurav Kukade

Posted on • Edited on

     

How Do I Compare Strings In Java

This article is originally published athttps://coderolls.com/compare-strings-in-java/

In this article you are going to learn how to compare strings. What problem occurs when you compare string usingequals to (=)operator.

Introduction

TheString is a special Class in Java. We use String regularly in Java programs, so comparing two string is a common practice in Java. In this article I tried to answer the most common questions about the string, ‘How do I compare strings in Java?’

Comparing strings is very helpful the processes like authentication, sorting, reference matching, etc.

I have listed 3 ways to compare strings in Java.

  1. Usingequals() method ( comparing the content)

  2. Using== operator (comparing the object reference)

  3. UsingcompareTo() method (comparing strings lexicographically)

1. Compare strings usingequals() method

In this way, I am using.equals() instance method of the String class. Originally.equals() method is theObject class method, String class overrides it.

**equals()**method the compare two strings for value equality, weather they are logically equal.

equals() method in String class takes another string a parameter and compare it with the specified string, it returnstrue if and only if the parameter string is not null and contains the same characters as the specified string.

public boolean equals(Object anObject)It compare this string with the argument strings and return true if the argument is not null and contains the same character as the specified string.param -another stringreturns -true - if argument is not null and it contains same characters as the specified stringfalse - if the argument is null or it does not contain same characters as the specified stringex. firstString.equals(secondString)// returns true if and only if the secondString is not null and contains the same characters as firstString.
Enter fullscreen modeExit fullscreen mode

I have given the program to compare string usingequals() method below

/** * A Java program to compare two strings using equsls() * and equalsIgnoreCase() method of the String. *  * @author Gaurav Kukade at coderolls.com */public class CompareUsingEquals {  public static void main(String[] args) {    String firstString = "coderolls";    String secondString = "javablog";    String thirdString = "coderolls";    String fourthString = "CodeRolls";    System.out.println("Comparing strings using equals() and equalsIgnoreCase() method\n");    // Using equals() method    System.out.print("firstString.equals(secondString) : ");    System.out.println(firstString.equals(secondString));    System.out.print("firstString.equals(thirdString) : ");    System.out.println(firstString.equals(thirdString));    /*     * Using equalsIgnoreCase() method to ignore     * case consideration (i.e. Capital or small) of both the strings.     */    System.out.print("firstString.equalsIgnoreCase(fourthString) : ");    System.out.println(firstString.equalsIgnoreCase(fourthString));  }}
Enter fullscreen modeExit fullscreen mode

Output:

Comparing strings using equals() and equalsIgnoreCase() methodfirstString.equals(secondString) : falsefirstString.equals(thirdString) : truefirstString.equalsIgnoreCase(fourthString) : true
Enter fullscreen modeExit fullscreen mode

2. Compare strings using== operator

In String,**==**operator is used to comparing the reference of the given strings, whether they are referring to the same objects.

When you compare two strings using== operator, it will returntrue if the string variables are pointing toward the same java object, else it will returnfalse.

I have given a Java program to compare using== operator below

/** * A Java program to compare strings using == operator. *  * == operator ckecks weather both the strings referring * to the same String Object. *  * @author Gaurav Kukade at coderolls.com */public class CompareUsingEqualsToOperator {  public static void main(String[] args) {    String firstString = "coderolls";    String secondString = "javablog";    String thirdString = "coderolls";    // creating new String object with the same value as firstString or thirdString    String fourthString =  new String("coderolls");    System.out.println("Comparing strings using == operator \n");    System.out.print("firstString == secondString : ");    System.out.println(firstString == secondString);    /*     * Here firstString and thirdString is referring to the same String object     * hence it will print 'true'.     */    System.out.print("firstString == thirdString : ");    System.out.println(firstString == thirdString);    /*     * Here firstString and fourthString have same value     * but they are referring to the different String object.     *      * hence it will print 'false'     */    System.out.print("firstString == fourthString : ");    System.out.println(firstString == fourthString);  }}
Enter fullscreen modeExit fullscreen mode

Output:

Comparing strings using == operator firstString == secondString : falsefirstString == thirdString : truefirstString == fourthString : false
Enter fullscreen modeExit fullscreen mode

Problem with using== operator for string comparison

Most of the beginner Java developers commit this mistake by comparing two strings using the == operator.

Logically, they have to check whether both the string contains the same character sequence or not.

In Java String, the== operator used to check the reference of both the string objects andequals() method used to check the value equality of both strings.

== – checks reference equality

equals() – checks the value equality

When we assign a string value to the string variable JVM will check if the string with the equal value already present in the string pool or not. If it is not present in the string pool, it will be added to the constant pool and the reference to that string object is returned.

If it is present in the string pool, the reference to the memory address of that string object is returned.

The following image shows the pictorial explanation of the same.

'firstString' pointing towards the

‘firstString’ pointing towards the “coderolls” string in string pool

If we are assigning the equal value to another string variable, JVM checks if the string with that value is present in the string constant pool or not.

Since the string object with that value is already created in the previous step. Another string variable starts referring to the previously created string object instance.

The following image shows the pictorial explanation for the same

'firstString' and 'secondString' pointing towards the

‘firstString’ and ‘secondString’ pointing towards the “coderolls” string in string pool

When we create string using thenew operator, a new string object is created and stored in the Java heap space.

'firstString' and 'secondString' pointing towards the

‘firstString’ and ‘secondString’ pointing towards the “coderolls” string in string pool and ‘thirdString’ pointing towards the “coderolls” in java heap space.t

3. Compare strings usingcompareTo() method

compareTo() method is used to compare two strings lexicographically. i.e. Alphabetically.

compareTo() method compares the character sequence of the argument string with the character sequence of the specified string.

Showing argument string and specified string

Showing argument string and a specified string.

It returns a negative integer if the argument string is lexicographically greater than the specified string. i.e if the argument string follows the specified string. ( argument String > specified String )

It returns positive integer if the argument string is lexicographically smaller than the specified string. i.e. If the argument string precedes the specified string. ( argument String < specified String )

It returns zero if both the strings are lexicographical equals. ( argument String = specified String )

If you want to ignore the cases of both the string usecompareToIgnoreCase() method.

I have given a program for comparing strings using thecompareTo() method. It also consists a case for ignoring the cases withcompareToIgnoreCase() method.

 * A Java program to compare strings using compareTo() * and compareToIgnoreCase() method. *  * compareTo() compare strings lexicograpgically. *  * @author Gaurav Kukade at coderolls.com */public class CompareUsingCompareTo {  public static void main(String[] args) {    String firstString = "java";    String secondString = "coderolls";    String thirdString = "sql";    String fourthString = "CodeRolls";    System.out.println("Comparing strings using compareTo() and compareToIgnoreCase() method\n");    // Using compareTo() method    System.out.print("firstString.compareTo(secondString) : ");    System.out.println(firstString.compareTo(secondString));    System.out.print("firstString.compareTo(thirdString) : ");    System.out.println(firstString.compareTo(thirdString));    /*     * Using compareToIgnoreCase() method to ignore     * case consideration (i.e. Capital or small) of both the strings.     */    System.out.print("secondString.compareToIgnoreCase(fourthString) : ");    System.out.println(secondString.compareToIgnoreCase(fourthString));  }}
Enter fullscreen modeExit fullscreen mode

Output:

Comparing strings using compareTo() and compareToIgnoreCase() methodfirstString.compareTo(secondString) : 7firstString.compareTo(thirdString) : -9secondString.compareToIgnoreCase(fourthString) : 0
Enter fullscreen modeExit fullscreen mode

I have written a detailed article onhow to compare strings lexicographically in java. In this article, I have also created a user-defined method to compare two strings lexicographically.

Conclusion

We can compare strings using the ways given below

  1. Usingequals() method :equals() method in the strings used to check the string value equality whether they contain the same character sequence.

  2. Using== operator :== operator used to check the reference equality of the two strings, whether they are pointing towards the same string object.

  3. UsingcompareTo() method :compareTo() method used to check the strings lexicographically. I.e alphabetically. Check the detailed articles onHow to compare strings lexicographically.

Most of the beginner java developers do mistakes while comparing strings. They actually want to check the content of the string but they use== operator to check it.

It is always advised to useequals() method to compare the string on the basis of its content.

If you have any queries about the code blocks given above, please write it down in the comment section below. Also. let me know if you have any other way to compare two strings in java in the comment section.

This article is originally published athttps://coderolls.com/compare-strings-in-java/

Related Article

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
ben profile image
Ben Halpern
A Canadian software developer who thinks he’s funny.
  • Email
  • Location
    NY
  • Education
    Mount Allison University
  • Pronouns
    He/him
  • Work
    Co-founder at Forem
  • Joined

Super useful post, thanks!

CollapseExpand
 
gauravkukade profile image
Gaurav Kukade
A Java Developer 👨‍💻 and author ✍️ at coderolls.com , Exploring Javascript! 💻
  • Location
    Pune, India
  • Education
    Bachelor of Engineering in Electronics and Telecommunication
  • Work
    Java Developer
  • Joined
• Edited on• Edited

Your welcome, Ben!

I did this mistake many times in the beginning, and that's why I have shared this blog with all.

Thanks for your response. 🙂

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A Java Developer 👨‍💻 and author ✍️ at coderolls.com , Exploring Javascript! 💻
  • Location
    Pune, India
  • Education
    Bachelor of Engineering in Electronics and Telecommunication
  • Work
    Java Developer
  • Joined

More fromGaurav Kukade

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp