Java/J2EE and SOA (348 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (345 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

5 Ways For Comparing Two Strings In Java

Last updated on Jun 06,2023 23.6K Views


String Manipulation can be of immense help in different domains. It may help in text analytics, data matching, data mining etc. In this article we will focus on comparing two strings in Java which again different purposes for string manipulation. Following are the pointers that would be discussed here

So let us get started then,

Comparing Two Strings In Java

A sequence of characters can be defined as a String. They are immutable, i.e. they cannot be modified once created. There are various methods to compare two strings in java, as seen below.

String Equals Method 

The strings are compared on the basis of the values present in the string. The method returns true if the values of the two strings are same, and false, if the values don’t match.


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 and Roll :false

Comparing rock and Rock :false

Comparing Rock and Roll :false

Comparing Rock and Rock :true

Let us continue with the second bit of this article,

String Equals Ignore Case

This method compares the two strings, and does not take into account the case of the string( lower or upper). Returns true if the values are equal and not null. 

</p>
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 and Roll :false

Comparing rock and Rock :true

Comparing Rock and Roll :false

Comparing Rock and Rock :true

Let us move further with the next bit of this comparing two strings in Java article,

Object Equals Method

If the arguments are equal to the each other, the method returns true, else, it returns false. If both the arguments present are null, the output returned is true. If a single argument is of a null value, the output returned is false.


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 and Roll :false

Comparing Roll and Roll :true

Comparing Rock and null :false

Comparing null and null :true

Let us move further now

String Compare To Method 

In this method, the input strings are compared to each other. The value returned after comparison is as follows:

  • if(str1>str2), a positive value is returned.
  • If(str1==str2), 0 is returned.
  • If(str1<str2), a negative value is returned.

Code


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 Java article,

Using Double Equal To Operator 

This method should be avoiding while comparing two string values. The major differences between the equals() and == operator is given below:

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

  • == operator is used for reference comparison, while on the other hand, equals() method is used for content comparison.

== operator is avoided, since it checks for reference equality, i.e. if the strings point to the same object or not.

Code


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 methods mentioned in the article provide a meticulous way to compare two strings in the programming language of java.

Discover how to create mobile apps that look and feel great on any platform with a comprehensive Flutter Course.

Thus we have come to an end of this article on ‘Array Of Objects in Java’. If you wish to learn more, check out the Java Online Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this article and we will get back to you as soon as possible.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 4th May,2024

4th May

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 25th May,2024

25th May

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

5 Ways For Comparing Two Strings In Java

edureka.co