6 Ways for Comparing two strings in Java

Safalta Expert Published by: Aryan Rana Updated Wed, 16 Nov 2022 11:40 PM IST

Highlights

A String is a term that describes a group of characters. They cannot be changed after being produced since they are immutable.

Table of Content
Java Comparison of Two Strings
Method String Equals
Equals String Ignore Case
Object = Method
Method For Comparing Strings

Double Equal To Operator Utilization


In many different fields, string manipulation can be incredibly beneficial. Data mining, text analytics, and data matching could all benefit from it. In this post, we'll concentrate on comparing two strings in Java for various string manipulation reasons.

Download these FREE Ebooks:
1. Introduction to Digital Marketing
2. Website Planning and Creation


You can check other related blogs below:
1. Powerful SEO Techniques to rank in Google
2. How to get powerful SEO backlinks? Top 10 Tips to get Backlinks

Free Demo Classes

Register here for Free Demo Classes


3. Search Intent - All You Should know
4. What is page experience in Digital marketing?

5. SEO Vs PPC: Which is beneficial?
6. 7 Tips for combine Website Content to Improve SEO
7. 6 Reasons Email Marketing increase holiday sales
8. 6 SEO hacks to revive your Website



Java Comparison of Two Strings

A String is a class of characters that can be defined. They are unchangeable after creation because they are unchanging. As can be seen below, there are several ways to compare two strings in Java.


Method String Equals

The values contained in the strings are used to compare the strings. If the values of the two strings match, the procedure returns true; otherwise, it returns false.
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Main {
public static void main(String args[])
{
String str1 = new String("Rock");
String str2 = new String("Roll");
String str3 = new String("rock");
String str4 = new String("Rock");
String str5 = new String("Roll");
//comparing the strings
System.out.println("Comparing " + str1 + " and " + str2
+ " : " + str1.equals(str2));
System.out.println("Comparing " + str3 + " and " + str4
+ " : " + str3.equals(str4));
System.out.println("Comparing " + str4 + " and " + str5
+ " : " + str4.equals(str5));
System.out.println("Comparing " + str1 + " and " + str4
+ " : " + str1.equals(str4));
}
}


Output:

Comparing Rock & Roll: False

comparing rock and Rock: False

Comparing Rock & Roll: False

comparing Rock and Rock: True

Let's proceed with the second section of this article.


Equals String Ignore Case

The case of the string is not taken into account when this function compares the two strings ( lower or upper). If the values are both equal and not null, the result is true.
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

 


public class Main {
public static void main(String args[])
{
String str1 = new String("Rock");
String str2 = new String("Roll");
String str3 = new String("rock");
String str4 = new String("Rock");
String str5 = new String("Roll");
//Comparing Strings
System.out.println("Comparing " + str1 + " and " + str2
+ " : " + str1.equalsIgnoreCase(str2));
System.out.println("Comparing " + str3 + " and " + str4
+ " : " + str3.equalsIgnoreCase(str4));
System.out.println("Comparing " + str4 + " and " + str5
+ " : " + str4.equalsIgnoreCase(str5));
System.out.println("Comparing " + str1 + " and " + str4
+ " : " + str1.equalsIgnoreCase(str4));
}
}


Output:

Comparing Rock & Roll: False

comparing rock with Rock: True

Comparing Rock & Roll: False

comparing Rock and Rock: True

Let's continue with the following section of this post about comparing two strings in Java,


Object = Method

The method returns true if the parameters are equal to one another, and false otherwise. The output is true if both of the given parameters are null. The output is false if any one input has a null value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.*;
public class Main {
public static void main(String args[])
{
String str1 = new String("Rock");
String str2 = new String("Roll");
String str3 = new String("Roll");
String str4 = null;
String str5 = null;
System.out.println("Comparing " + str1 + " and " + str2
+ " : " + Objects.equals(str1, str2));
System.out.println("Comparing " + str2 + " and " + str3
+ " : " + Objects.equals(str2, str3));
System.out.println("Comparing " + str1 + " and " + str4
+ " : " + Objects.equals(str1, str4));
System.out.println("Comparing " + str4 + " and " + str5
+ " : " + Objects.equals(str4, str5));
}
}


Output:

Comparing Rock & Roll: False

Comparing Roll and Roll: True

Comparing Rock and null: False

comparing null and null: True

Now, let's continue.


Method For Comparing Strings

The input strings are compared to one another in this procedure. Following the comparison, the value is as follows:
  • A positive value is returned if (str1>str2) is true.
  • 0 is given if (str1==str2) is true.
  • A negative value is returned if (str1str2) is true.
 

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.*;
public class Main {
public static void main(String args[])
{
String str1 = new String("Rock");
String str2 = new String("Pop");
String str3 = new String("Roll");
String str4 = new String("Roll");
System.out.println("Comparing " + str1 + " and " + str2
+ " : " + str1.compareTo(str2));
//Comparing String 3 = String 4
System.out.println("Comparing " + str3 + " and " + str4
+ " : " + str3.compareTo(str4));
System.out.println("Comparing " + str2 + " and " + str4
+ " : " + str2.compareTo(str4));
}
}


Output:

Comparing Rock and Pop:2

Comparing Roll and Roll :0

Comparing Pop and Roll:-2

This brings us to the final bit of this comparing two strings in a Java article,


Double Equal To Operator Utilization

Avoid using this approach when comparing two string values. The following list highlights the key distinctions between the equals() and == operators:

While == is an operator, equals() is a method.

While the equals() technique is used for content comparison, the == operator is used for reference comparison.

Since the == operator determines whether two strings point to the same object or not, reference equality is checked rather than verified.

 

Code

1
2
3
4
5
6
7
8
9
10
import java.util.*;
public class Main {
public static void main(String[] args)
{
String str1 = new String("Rock");
String str2 = new String("Rock");
System.out.println(str1 == str2);
System.out.println(str1.equals(str2));
}
}


Output:

False

True


The techniques described in the article give programmers of the Java programming language a thorough approach to comparing two strings.


  In Java, there are three methods for comparing strings: with the equals() method. Utilizing the == Operator. using the compareTo() method.

How can two sets of strings be compared in Java?

Java's util. equals() function. The Set class is utilised to compare and validate the equivalence of an Object with a Set. If the sets are the same size and have the same number of elements, the function returns true.

Can you compare two strings in Java using ==?

Java compares two strings using the == operator, also referred to as the equality operator.

Related Article

CTET Answer Key 2024: दिसंबर सत्र की सीटेट परीक्षा की उत्तर कुंजी जल्द होगी जारी, जानें कैसे कर सकेंगे डाउनलोड

Read More

CLAT 2025: दिल्ली उच्च न्यायालय ने एनएलयू को दिया क्लैट परीक्षा के नतीजों में संशोधन का आदेश, जानें पूरा मामला

Read More

UP Police: यूपी पुलिस भर्ती का आवेदन पत्र डाउनलोड करने का एक और मौका, यूपीपीआरपीबी ने फिर से सक्रिया किया लिंक

Read More

JEE Advanced 2025: जेईई एडवांस्ड के लिए 23 अप्रैल से शुरू होगा आवेदन, जानें कौन कर सकता है पंजीकरण

Read More

UPSC CSE Mains 2024 Interview Schedule out now; Personality tests from 7 January, Check full timetable here

Read More

Common Admission Test (CAT) 2024 Result out; 14 Students Score 100 Percentile, Read here

Read More

CAT Result: कैट परीक्षा के परिणाम जारी, इतने उम्मीदवारों ने 100 पर्सेंटाइल स्कोर किए हासिल; चेक करें रिजल्ट

Read More

CBSE: डमी प्रवेश रोकने के लिए सीबीएसई का सख्त कदम, 18 स्कूलों को जारी किया कारण बताओ नोटिस

Read More

Jharkhand Board Exam Dates 2025 released; Exams from 11 February, Check the full schedule here

Read More