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

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Selenium
  • Manual Testing
  • SQL Tutorial For Beginners
  • Difference
  • Tools
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Tutorial » String Comparison in Java

String Comparison in Java

Last Updated on: July 8, 2023 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • String Comparison In Java
  • By Using == operator
  • Program: How to compare Strings using the == operator?
  • By equals() method
  • Program: How to compare the Strings content in Java?
  • By compareTo()
  • Program: Write a Java Program to compare Two Strings using the compareTo() method.

String Comparison in Java: A String in Java is a sequence of characters. It’s important to know that strings cannot be changed once they are created. In Java, strings are often used, and comparing strings is a common task. You might need to compare strings in Java in many situations, like organizing a list of words alphabetically or checking if a certain word is already in a database.

In this blog post, we will explore different ways to compare strings efficiently and quickly in Java. We will go over each method in detail and provide practical examples to help you understand them better. So let’s dive right in and get started!

String Comparison In Java

We can compare two string objects in 3 ways in Java programming language. That is:

  • By Using == operator
  • By using equals() method
  • By Using compareTo() method
String Comparison In Java Explanation
String Comparison In Java Explanation

By Using == operator

If you want to compare the reference of the string objects but not the string object content, then, in that case, you can use the == operator. After comparing if both the operands have the same reference, then, in that case, it will return true; otherwise, it will return false.

Program: How to compare Strings using the == operator?
  
public class String_Comparison_Using_Operator 
{
   public static void main(String[] args) 
   {
      String str1="Software Testingo";
      String str2="Software Testingo";
      String str3 = new String("SoftwareTestingo");

      // String Comparison between Str1 and Str2
      if (str1==str2)
      {
         System.out.println("Str1 & Str2 Reference Is Same");
      }
      else
      {
         System.out.println("Reference Of Both the String Object are Not Same");
      }

      //String Comparison between Str1 and Str3
      if (str1==str3)
      {
         System.out.println("Str1 & Str3 Reference Is Same");
      }
      else
      {
         System.out.println("Reference Of Both the String Object are Not Same");
      }
   }
}

Output:

Str1 & Str2 Reference Is Same
Reference Of Both the String Object are Not Same

By equals() method

If you want to compare two string objects’ contents, then you can use the equals() method. equals() is an object class method. Still, the string class overrides the equals() method and provides the below format of equals() methods

  • public boolean equals(Object obj): Compares the string with the specified object. Returns true if the actual content is equal; otherwise, returns false.
  • Public Boolean equalsIgnoreCase(String str): Compares the string’s actual content with the specified string’s content. Returns true if two string’s content is equal, ignoring case, otherwise returns false.
Program: How to compare the Strings content in Java?
package com.SoftwareTestingO.Java.basics;
public class String_Comparison_Using_Equal_Method 
{
   public static void main(String[] args) 
   {
      String str1="Software Testingo";
      String str2="Software Testingo";
      String str3 = new String("Software Testingo");
      String str4="Software";
      String str5="SOFTWARE TESTINGO";

      //Comparing Str1 & Str2
      if(str1.equals(str2))
      {
         System.out.println("Both Str1 & Str2 have the same value");
      }
      else
      {
         System.out.println("Both have the different value");
      }
      //Comparing Str1 & Str3
      if(str1.equals(str3))
      {
         System.out.println("Both Str1 & Str3 have the same value");
      }
      else
      {
         System.out.println("Both have the different value");
      }
      //Comparing Str2 & Str4
      if(str2.equals(str4))
      {
         System.out.println("Both Str2 & Str4 have the same value");
      }
      else
      {
         System.out.println("Both str2 & str4 have the different value");
      }
      //Comparing Str2 & Str5, Here content same but different in case
      if(str2.equals(str5))
      {
         System.out.println("Both Str2 & Str5 have the same value");
      }
      else
      {
         System.out.println("Both Str2 & Str5 have the same value but different in case");
      }

      //Comparing Str2 & Str5, Here content same but ignore case
      if(str2.equalsIgnoreCase(str5))
      {
         System.out.println("Both Str2 & Str5 have the same value");
      }
      else
      {
         System.out.println("Both have the same value but different in case");
      }

   }
}

Output:

Both Str1 & Str2 have the same value
Both Str1 & Str3 have the same value
Both str2 & str4 have the different value
Both Str2 & Str5 have the same value but different in case
Both Str2 & Str5 have the same value

By compareTo()

compareTo() method compares the two strings lexicographically, i.e., character by character. The string class provides the following formats of the compareTo() method.

  • public int compareTo(String str)
  • public int compareToIgnoreCase(String str)

It returns 0 if the argument string is equal to this string, less than 0 if it is lexicographically less than the string argument, and greater than 0 if it is lexicographically greater than the string argument.

Program: Write a Java Program to compare Two Strings using the compareTo() method.
package com.SoftwareTestingO.Java.basics;
class string_comparison
{
   String str1="Software Testingo";
   String str2="Software Testingo";
   String str3="Software";
   String str4="Testingo";

   public void stringcompare()
   {
      //return 0, because content are same.
      System.out.println(str1.compareTo(str2));

      //return greater than 0, because str1 
      //lexicographically greater than str2.
      System.out.println(str1.compareTo(str3));

      //return less than 0, because str1 
      //lexicographically less than str2.
      System.out.println(str3.compareTo(str4));
   }
}
public class String_Comparison_Using_CompareTo 
{
   public static void main(String[] args) 
   {
      string_comparison obj=new string_comparison();
      obj.stringcompare();
   }
}

Output:

0
9
-1

Conclusion:

In this guide, you learned the different methods for comparing strings in Java. You saw different ways to compare strings, each with its specific use. However, the methods Objects.equals() and String.equals() are versatile and can be used in many situations to get the best results for your Java program.

    Difference Between JDK JRE and JVM in Details
    Difference Between JDK, JRE and JVM in Details
    Polymorphism In Java
    Polymorphism In Java
    Comparable and Comparator Interface in Java
    Comparable and Comparator Interface in Java
    Identifiers in Java
    Identifiers in Java
    Java Data Types
    Naming Conventions & Data Types In Java
    What is Java Interpreter Compiler in Java
    What is Java Interpreter Compiler in Java?
    Java Inner Class With Example
    Java Inner Class With Example
    How to Set Java_Home Variable On Windows 10
    How to Set Java_Home Variable On Windows 10 Step By...
    Three Dimensional Array In Java
    Three Dimensional Array In Java
    Java For Loop Syntax & Examples
    Java For Loop Syntax & Examples

    Filed Under: Java Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

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

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Footer

    Java Tutorial for Beginners | Selenium Tutorial | Manual Testing Tutorial | SQL Tutorial For Beginners | GitHub Tutorial For Beginners | Maven Tutorial

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers