Javatpoint Logo
Javatpoint Logo

Understanding String Comparison Operator in Java

In the world of programming, string comparison is a common operation that involves determining whether two strings are equal or if one string comes before or after another in lexicographical order. Java, being one of the most popular programming languages, provides various ways to perform string comparison. One of the fundamental methods of comparing strings in Java is through the use of string comparison operators. In this article, we will delve into the details of the string comparison operator in Java and explore its functionality.

String Comparison in Java

In Java, strings are objects of the String class, and they are compared using the equals() method or the string comparison operators. The string comparison operators in Java are as follows:

The equality operator (==) compares the memory addresses of the two string objects, rather than their actual contents. It checks if the two string references point to the same memory location. In other words, it checks for reference equality.

Example:

In the above example, str1 and str2 both reference the same string literal in the string pool, so str1 == str2 returns true. However, str1 and str3 refer to different objects, even though their content is the same, so str1 == str3 returns false.

equals() Method

The equals() method compares the actual contents of two string objects to determine their equality. It overrides the default implementation of equals() in the Object class and provides a meaningful comparison for strings.

Example:

In the above example, str1.equals(str2) and str1.equals(str3) both return true because the equals() method compares the content of the strings, not their memory addresses.

compareTo() Method

The compareTo() method is used to compare two strings lexicographically. It returns an integer value based on the comparison result:

If the strings are equal, it returns 0.

If the invoking string comes before the argument string lexicographically, it returns a negative value.

If the invoking string comes after the argument string lexicographically, it returns a positive value.

Example:

In the above example, str1.compareTo(str2) returns -1 because "apple" comes before "banana" in lexicographical order.

Conclusion

String comparison is a crucial operation in Java programming, and understanding the string comparison operator and its alternatives is essential for writing reliable and accurate code. The string comparison operator (==) compares the memory addresses of string objects, while the equals() method compares the actual content of the strings. Additionally, the compareTo() method allows us to determine the lexicographical ordering of strings. By utilizing these techniques appropriately, Java developers can perform string comparison effectively in their applications.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA