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

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
  • Java Program
  • Selenium
  • Selenium Programs
  • Manual Testing
  • Difference
  • Tools
  • SQL
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Tutorial » String Comparison in Java

String Comparison in Java

Last Updated on: August 15, 2020 By Softwaretestingo Editorial Board

What We Are Learn On This Post

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

String Comparison In Java

In Java programming language, we can compare two string objects in 3 different ways. 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.

  
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 object’s contents, then you can use the equals() method. equals() is an object class method. Still, string class overrides the equals() method and provide 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 actual content of the string with the content of the specified string. Returns true if two string’s content is equal, ignoring case, otherwise returns false.
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 this string is lexicographically less than the string argument and greater than 0 if this string is lexicographically greater than the string argument.

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

    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

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