11/6/2023 0 Comments Java string compare![]() You can see that the method treats them as unequal. Here, you have defined two strings called s1 and s2, and the only difference between the values stored inside them is that one string starts with a lowercase “h” and the other one starts with an uppercase “H”. Let’s check out an example for that as well. String Comparison using the String.equalsIgnoreCase() MethodĪs discussed, the String.equals() method treats strings with the only difference in cases being unequal. Let’s see the result.įrom the above output, it’s quite clear that the Strings.equals() method treats both objects and data-types the same way and only checks for the value stored inside rather than treating objects differently, like the equality operator. Finally, it also tried to compare two string objects with the same values and different values. Moving on, you have also compared strings that are equal and one of them is a double-quoted variable while the other one is an object defined using the new operator. You have compared strings that are equal and both defined using double-quotes. Here, you must try to compare all the combinations. Here, you have defined 5 strings, two of them directly using the double-quotes and 3 as objects using the new operator. To treat them as equal, you can use the Strings.equalsIgnoreCase() method that this article will discuss after this method. Also, another point to note is that it is case-sensitive, so even if the two strings to be compared are different in terms of just the cases, they will be treated as unequal. Irrespective of the fact whether the strings are created using double-quotes or as objects using a new operator, it will always compare with the content. The Strings.equals() method in Java is used to compare the content or value stored in the strings. String Comparison Using the Strings.equals() Method Hence, for comparing strings, it’s always better to avoid the equality operator and choose other methods that are discussed below. ![]() This is the reason it shows s4 and s1 as equal because they refer to the same memory location. This is so because this time, the equality operator is trying to compare objects and simply checks whether they refer to the same memory location or not. Even though the strings s1 and s2 store the same values, it shows that they are not equal. Here, as you can see, that the output is not what was expected. ![]() Also, you have created a new string object called s4, which simply refers to s1. In the above example, you can have initialized the strings as objects. Let’s create them as objects using a new operator in the next example. This is so because you have defined the strings using double-quotes here. This means that the equality operator is working fine in this case. You can see that the outputs are correct. This example has simply used the equality operator along with if-else conditions to print the results. The aim is to check whether the values in s1 and s2 are the same or not, and also whether the values in s1 and s3 are equal or not. In the example above, you have seen the creation of 3 string variables using double-quotes where s1 and s2 have the same string values which differ from the ones stored in s3. ![]() Let’s verify both the scenarios using simple examples. However, if you define strings directly with double-quotes, then you can compare the actual values stored inside them. In simpler words, when you define strings as objects, they won’t compare with the actual value but only the references. Please note that the equality operator does not compare the actual value in Java, it only compares the references. If it finds out that both the objects points or refers to the same thing, it returns true, else false. The equality operator in Java is used to compare two or more objects. String Comparison using the Equality (=) Operator.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |