Comparing strings is a fundamental task in Java programming, crucial for various operations from input validation to sophisticated searching algorithms. At compare.edu.vn, we provide detailed comparisons and insights to help you make informed decisions. This article explores diverse methods for string comparison in Java, providing you with a comprehensive understanding of each approach.
1. What Are The Different Ways To Compare Strings In Java?
Yes, strings can be compared in Java using several methods, each serving specific purposes. These methods include equals()
, equalsIgnoreCase()
, compareTo()
, and Objects.equals()
. The choice of method depends on whether case sensitivity is required, or if you need to handle null values gracefully. Understanding each method helps in writing efficient and reliable code.
1.1 Using the equals()
Method
The equals()
method is the most straightforward way to compare strings in Java. It checks if the content of two strings is identical, considering case sensitivity. If the strings match exactly, it returns true
; otherwise, it returns false
. This method is reliable and widely used for basic string comparison needs.
Example:
String str1 = "Hello";
String str2 = "Hello";
String str3 = "World";
System.out.println(str1.equals(str2)); // Output: true
System.out.println(str1.equals(str3)); // Output: false
In this example, str1.equals(str2)
returns true
because both strings have the same content. However, str1.equals(str3)
returns false
because the strings are different.
1.2 Using the equalsIgnoreCase()
Method
The equalsIgnoreCase()
method is similar to equals()
, but it ignores case sensitivity. This method is useful when you want to compare strings without considering whether the characters are uppercase or lowercase. It returns true
if the strings are the same, regardless of case, and false
otherwise.
Example:
String str1 = "Java";
String str2 = "java";
System.out.println(str1.equalsIgnoreCase(str2)); // Output: true
Here, str1.equalsIgnoreCase(str2)
returns true
because the method ignores the case difference between "Java"
and "java"
.
1.3 Using the compareTo()
Method
The compareTo()
method compares two strings lexicographically, meaning it compares them based on the Unicode values of their characters. This method returns an integer value that indicates the relationship between the two strings:
- If
string1
is lexicographically greater thanstring2
, it returns a positive value. - If
string1
is lexicographically equal tostring2
, it returns 0. - If
string1
is lexicographically less thanstring2
, it returns a negative value.
Example:
String str1 = "Java";
String str2 = "Domain";
System.out.println(str1.compareTo(str2)); // Output: 6
In this case, str1.compareTo(str2)
returns 6 because "Java"
comes after "Domain"
lexicographically. The exact value represents the difference in Unicode values between the first differing characters.
1.4 Using the Objects.equals()
Method
The Objects.equals()
method is a utility method that checks if two objects are equal, handling null
values safely. This method returns true
if both arguments are null
or if the first argument’s equals()
method returns true
when passed the second argument. It returns false
if only one argument is null
.
Example:
import java.util.Objects;
public class StringComparison {
public static void main(String[] args) {
String str1 = "Java";
String str2 = null;
System.out.println(Objects.equals(str1, str2)); // Output: false
System.out.println(Objects.equals(null, null)); // Output: true
}
}
Here, Objects.equals(str1, str2)
returns false
because one of the strings is null
. Objects.equals(null, null)
returns true
because both strings are null
.
This image illustrates the different string comparison methods available in Java.
2. What Are The Key Differences Between equals()
And ==
When Comparing Strings?
The equals()
method and the ==
operator are both used for comparison, but they operate differently, especially when comparing strings. The equals()
method compares the content of the strings, while the ==
operator compares the references of the string objects. Understanding this distinction is crucial for avoiding common pitfalls in Java programming.
2.1 Comparing Content vs. References
The primary difference between equals()
and ==
is that equals()
compares the actual characters in the strings, whereas ==
checks if the two string variables point to the same memory location. In other words, equals()
verifies if the strings have the same value, while ==
verifies if they are the same object.
Example:
String str1 = new String("Hello");
String str2 = new String("Hello");
System.out.println(str1.equals(str2)); // Output: true
System.out.println(str1 == str2); // Output: false
In this example, str1.equals(str2)
returns true
because the content of both strings is the same. However, str1 == str2
returns false
because str1
and str2
are two different objects in memory, even though they have the same value.
2.2 String Literals and the String Pool
Java uses a string pool to store string literals. When you create a string using a literal (e.g., "Hello"
), Java checks if an identical string already exists in the pool. If it does, the new variable will reference the existing string in the pool. This can lead to ==
returning true
for string literals, which can be confusing if you expect it to always behave like it does for objects created with new String()
.
Example:
String str1 = "Hello";
String str2 = "Hello";
System.out.println(str1.equals(str2)); // Output: true
System.out.println(str1 == str2); // Output: true
Here, str1 == str2
returns true
because both variables reference the same string in the string pool.
2.3 When to Use equals()
vs. ==
- Use
equals()
when you need to compare the content of strings. This ensures that you are checking if the strings have the same value, regardless of whether they are the same object. - Use
==
when you need to check if two string variables refer to the same object in memory. This is less common for string comparison but can be useful in specific scenarios where object identity is important.
2.4 Best Practices for String Comparison
To avoid confusion and ensure correct behavior, it is generally recommended to use the equals()
method for comparing strings in Java. This practice helps you focus on the content of the strings rather than their memory locations.
This image highlights the difference between using the equals() method and the == operator for string comparison in Java.
3. How Do You Perform Case-Insensitive String Comparison In Java?
Case-insensitive string comparison is essential when you need to compare strings without regard to the case of the characters. Java provides the equalsIgnoreCase()
method for this purpose. This method allows you to compare strings and determine if they are equal, ignoring differences in case.
3.1 Using the equalsIgnoreCase()
Method
The equalsIgnoreCase()
method compares two strings, ignoring case differences. It returns true
if the strings are equal when case is ignored, and false
otherwise. This method is straightforward and effective for case-insensitive comparisons.
Example:
String str1 = "Java";
String str2 = "java";
System.out.println(str1.equalsIgnoreCase(str2)); // Output: true
In this example, str1.equalsIgnoreCase(str2)
returns true
because the method ignores the case difference between "Java"
and "java"
.
3.2 Benefits of equalsIgnoreCase()
- Simplicity: The method is easy to use and understand.
- Readability: It clearly indicates that the comparison is case-insensitive.
- Efficiency: It is optimized for performance, making it a good choice for frequent comparisons.
3.3 Alternatives for Case-Insensitive Comparison
While equalsIgnoreCase()
is the most common and recommended method, you can also achieve case-insensitive comparison by converting both strings to the same case (either uppercase or lowercase) and then using the equals()
method. However, this approach is less efficient and less readable than using equalsIgnoreCase()
.
Example (Less Efficient):
String str1 = "Java";
String str2 = "java";
System.out.println(str1.toLowerCase().equals(str2.toLowerCase())); // Output: true
This example converts both strings to lowercase before comparing them, which involves creating new string objects. The equalsIgnoreCase()
method is generally more efficient because it avoids this extra step.
3.4 Practical Applications of Case-Insensitive Comparison
Case-insensitive string comparison is useful in various scenarios, such as:
- Input Validation: When validating user input, you might want to ignore case to provide a more forgiving experience. For example, allowing users to enter
"yes"
,"Yes"
, or"YES"
and treating them all as the same response. - Searching: When searching for text in a document or database, you might want to ignore case to find all occurrences of a word, regardless of how it is capitalized.
- Configuration Files: When reading configuration files, you might want to ignore case to allow flexibility in how settings are specified.
This image illustrates how the equalsIgnoreCase() method works by ignoring case differences during string comparison.
4. What Is Lexicographical String Comparison And How Does compareTo()
Work?
Lexicographical string comparison involves comparing strings based on the Unicode values of their characters. The compareTo()
method in Java performs this type of comparison. Understanding how compareTo()
works is essential for sorting strings and determining their relative order.
4.1 Understanding Lexicographical Order
Lexicographical order is similar to alphabetical order but includes all Unicode characters, not just letters. When comparing strings lexicographically, Java compares the characters at each position until it finds a difference or reaches the end of one of the strings.
4.2 How compareTo()
Works
The compareTo()
method returns an integer value that indicates the relationship between two strings:
- If
string1
is lexicographically greater thanstring2
, it returns a positive value. - If
string1
is lexicographically equal tostring2
, it returns 0. - If
string1
is lexicographically less thanstring2
, it returns a negative value.
Example:
String str1 = "Java";
String str2 = "Domain";
System.out.println(str1.compareTo(str2)); // Output: 6
In this example, str1.compareTo(str2)
returns 6 because "Java"
comes after "Domain"
lexicographically. The exact value represents the difference in Unicode values between the first differing characters ('J'
and 'D'
).
4.3 Detailed Explanation of the Comparison Process
- Character-by-Character Comparison: The
compareTo()
method compares the characters of the two strings one by one, starting from the first character. - Unicode Value Comparison: For each pair of characters, it compares their Unicode values.
- Difference Calculation: If the Unicode values are different, the method returns the difference between them.
- Handling Different Lengths: If one string is a prefix of the other, the method returns the difference in length between the two strings.
Example: Comparing "apple"
and "app"
String str1 = "apple";
String str2 = "app";
System.out.println(str1.compareTo(str2)); // Output: 1
In this case, "apple"
is lexicographically greater than "app"
because it is longer. The method returns the difference in length (5 – 3 = 2).
4.4 Practical Applications of compareTo()
The compareTo()
method is useful in various scenarios, such as:
- Sorting Strings: You can use
compareTo()
to sort an array or list of strings in lexicographical order. - Searching and Filtering: You can use
compareTo()
to find strings that are greater than, less than, or equal to a given string. - Data Validation: You can use
compareTo()
to ensure that strings conform to a specific format or range.
4.5 Using compareToIgnoreCase()
Java also provides a compareToIgnoreCase()
method that performs a case-insensitive lexicographical comparison. This method is useful when you want to compare strings without regard to the case of the characters.
Example:
String str1 = "Java";
String str2 = "java";
System.out.println(str1.compareToIgnoreCase(str2)); // Output: 0
In this example, str1.compareToIgnoreCase(str2)
returns 0 because the method ignores the case difference between "Java"
and "java"
.
This image illustrates how the compareTo() method works by comparing strings lexicographically based on Unicode values.
5. Can You Compare String With Null Values In Java Safely?
Comparing strings with null
values in Java requires careful handling to avoid NullPointerException
. The Objects.equals()
method provides a safe way to compare strings, as it handles null
values gracefully.
5.1 The Problem with null
Values
When you try to call the equals()
or compareTo()
method on a null
string, a NullPointerException
is thrown. This can cause your program to crash if not handled properly.
Example (Throws NullPointerException
):
String str1 = null;
String str2 = "Hello";
try {
System.out.println(str1.equals(str2)); // Throws NullPointerException
} catch (NullPointerException e) {
System.out.println("NullPointerException caught!");
}
5.2 Using Objects.equals()
for Safe Comparison
The Objects.equals()
method checks if either of the arguments is null
before performing the comparison. It returns true
if both arguments are null
, false
if only one argument is null
, and calls the equals()
method on the first argument if neither is null
.
Example (Safe Comparison):
import java.util.Objects;
public class StringComparison {
public static void main(String[] args) {
String str1 = null;
String str2 = "Hello";
System.out.println(Objects.equals(str1, str2)); // Output: false
System.out.println(Objects.equals(null, null)); // Output: true
}
}
In this example, Objects.equals(str1, str2)
returns false
because str1
is null
. Objects.equals(null, null)
returns true
because both strings are null
.
5.3 Benefits of Using Objects.equals()
- Null Safety: It prevents
NullPointerException
by handlingnull
values explicitly. - Readability: It clearly indicates that
null
values are being handled. - Simplicity: It provides a concise way to compare strings with
null
values.
5.4 Alternative Approaches
Another approach to handling null
values is to perform a null
check before calling the equals()
method. However, this approach is more verbose and less readable than using Objects.equals()
.
Example (Less Concise):
String str1 = null;
String str2 = "Hello";
if (str1 != null && str1.equals(str2)) {
System.out.println("Strings are equal.");
} else {
System.out.println("Strings are not equal."); // Output: Strings are not equal.
}
5.5 Best Practices for Handling null
Values
To avoid NullPointerException
and ensure correct behavior, it is recommended to use the Objects.equals()
method when comparing strings that might be null
. This practice helps you write more robust and reliable code.
This image illustrates how to safely compare strings with null values using Objects.equals() to avoid NullPointerException.
6. How Can You Compare Parts Of Strings In Java?
Comparing parts of strings in Java involves extracting substrings and then comparing them using the methods discussed earlier. This technique is useful when you need to compare specific segments of strings rather than the entire strings.
6.1 Using the substring()
Method
The substring()
method allows you to extract a portion of a string. It takes one or two arguments:
substring(int beginIndex)
: Returns the substring starting frombeginIndex
to the end of the string.substring(int beginIndex, int endIndex)
: Returns the substring starting frombeginIndex
and ending atendIndex - 1
.
Example:
String str = "Hello World";
String sub1 = str.substring(0, 5); // "Hello"
String sub2 = str.substring(6); // "World"
System.out.println(sub1); // Output: Hello
System.out.println(sub2); // Output: World
6.2 Comparing Substrings
After extracting the substrings, you can compare them using the equals()
, equalsIgnoreCase()
, or compareTo()
methods, depending on your specific requirements.
Example:
String str = "Hello World";
String sub1 = str.substring(0, 5); // "Hello"
String sub2 = "Hello";
System.out.println(sub1.equals(sub2)); // Output: true
In this example, sub1.equals(sub2)
returns true
because the substring "Hello"
is equal to the string "Hello"
.
6.3 Comparing Parts of Strings Case-Insensitively
You can also compare parts of strings case-insensitively using the equalsIgnoreCase()
method after extracting the substrings.
Example:
String str = "Hello World";
String sub1 = str.substring(0, 5); // "Hello"
String sub2 = "hello";
System.out.println(sub1.equalsIgnoreCase(sub2)); // Output: true
6.4 Using regionMatches()
for Substring Comparison
The regionMatches()
method provides another way to compare parts of strings. This method allows you to compare a specific region of one string to a specific region of another string.
Syntax:
boolean regionMatches(int toffset, String other, int ooffset, int len)
toffset
: The starting offset of the region in the current string.other
: The string to compare the region against.ooffset
: The starting offset of the region in theother
string.len
: The number of characters to compare.
Example:
String str1 = "Hello World";
String str2 = "World";
boolean match = str1.regionMatches(6, str2, 0, 5);
System.out.println(match); // Output: true
In this example, str1.regionMatches(6, str2, 0, 5)
returns true
because the substring "World"
in str1
matches the string "World"
.
6.5 Case-Insensitive regionMatches()
The regionMatches()
method also has a case-insensitive version:
Syntax:
boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
ignoreCase
: A boolean indicating whether to ignore case during the comparison.toffset
: The starting offset of the region in the current string.other
: The string to compare the region against.ooffset
: The starting offset of the region in theother
string.len
: The number of characters to compare.
Example:
String str1 = "Hello World";
String str2 = "world";
boolean match = str1.regionMatches(true, 6, str2, 0, 5);
System.out.println(match); // Output: true
In this example, str1.regionMatches(true, 6, str2, 0, 5)
returns true
because the substring "World"
in str1
matches the string "world"
when case is ignored.
This image illustrates how substrings are extracted from a string using the substring() method.
7. What Are Regular Expressions And How Can They Be Used For String Comparison?
Regular expressions are powerful patterns used to match character combinations in strings. In Java, you can use regular expressions to perform complex string comparisons, validate input, and search for specific patterns.
7.1 Understanding Regular Expressions
A regular expression is a sequence of characters that define a search pattern. Regular expressions can include:
- Literal Characters: Ordinary characters like
"a"
,"b"
,"1"
,"2"
. - Metacharacters: Special characters like
"."
,"*"
,"+"
,"?"
that have special meanings. - Character Classes: Sets of characters like
"[a-z]"
(lowercase letters),"[0-9]"
(digits). - Quantifiers: Specify how many instances of a character or group must be present in the input.
7.2 Using matches()
Method for Regular Expression Comparison
The matches()
method in Java’s String
class checks if a string matches a given regular expression. It returns true
if the string matches the pattern, and false
otherwise.
Example:
String str = "Hello123World";
String regex = "Hello\d+World"; // Matches "Hello" followed by one or more digits followed by "World"
System.out.println(str.matches(regex)); // Output: true
In this example, str.matches(regex)
returns true
because the string "Hello123World"
matches the regular expression "Hello\d+World"
. The d+
matches one or more digits.
7.3 Using Pattern
and Matcher
Classes
For more complex regular expression operations, you can use the Pattern
and Matcher
classes in the java.util.regex
package.
- Pattern: Represents a compiled regular expression.
- Matcher: An engine that performs match operations on a character sequence by interpreting a
Pattern
.
Example:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringComparison {
public static void main(String[] args) {
String str = "Hello123World";
String regex = "Hello(\d+)World"; // Captures the digits between "Hello" and "World"
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
System.out.println("Match found: " + matcher.group(0)); // Output: Match found: Hello123World
System.out.println("Captured digits: " + matcher.group(1)); // Output: Captured digits: 123
} else {
System.out.println("No match found.");
}
}
}
In this example, the regular expression "Hello(\d+)World"
captures the digits between "Hello"
and "World"
. The matcher.find()
method searches for a match, and matcher.group(1)
retrieves the captured digits.
7.4 Practical Applications of Regular Expressions
Regular expressions are useful in various scenarios, such as:
- Input Validation: Validating email addresses, phone numbers, and other types of input.
- Data Extraction: Extracting specific information from text, such as URLs, dates, and prices.
- Searching and Replacing: Finding and replacing text that matches a specific pattern.
- Data Transformation: Transforming data from one format to another.
7.5 Common Regular Expression Patterns
- Email Validation:
^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$
- Phone Number Validation:
^\d{3}-\d{3}-\d{4}$
- URL Extraction:
(https?://[\w./-]+)
This image illustrates the concept of regular expressions and how they are used to match patterns in strings.
8. How Does String Hashing Affect String Comparison In Java?
String hashing in Java involves generating a unique integer value (hash code) for each string. This hash code is used to optimize string comparison in data structures like HashMap
and HashSet
. Understanding how string hashing works and its impact on comparison is crucial for efficient Java programming.
8.1 Understanding String Hashing
In Java, the String
class overrides the hashCode()
method to provide a hash code for each string. The hash code is calculated based on the content of the string, ensuring that equal strings have the same hash code.
8.2 How Hashing Optimizes String Comparison
Hashing can significantly improve the performance of string comparison in data structures like HashMap
and HashSet
. These data structures use hash codes to quickly locate objects, reducing the need for逐个比较 comparisons.
HashMap
: When you put a key-value pair into aHashMap
, the key’s hash code is used to determine the bucket where the entry will be stored. When you retrieve a value, the key’s hash code is used to quickly find the correct bucket. If multiple keys have the same hash code (a collision), theequals()
method is used to compare the keys and find the exact match.HashSet
: When you add an element to aHashSet
, the element’s hash code is used to determine the bucket where the element will be stored. This allows theHashSet
to quickly check if an element already exists. If multiple elements have the same hash code, theequals()
method is used to compare the elements and ensure uniqueness.
8.3 Example: Using HashMap
with String Keys
import java.util.HashMap;
public class StringComparison {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("Java", 1);
map.put("Python", 2);
map.put("C++", 3);
System.out.println(map.get("Java")); // Output: 1
}
}
In this example, the HashMap
uses the hash codes of the string keys "Java"
, "Python"
, and "C++"
to quickly locate the corresponding values.
8.4 Importance of Consistent equals()
and hashCode()
It is crucial to ensure that the equals()
and hashCode()
methods are consistent. If two strings are equal according to the equals()
method, they must have the same hash code. If this is not the case, data structures like HashMap
and HashSet
will not work correctly.
8.5 Implementing equals()
and hashCode()
in Custom Classes
If you create your own class that needs to be used as a key in a HashMap
or stored in a HashSet
, you must override both the equals()
and hashCode()
methods.
Example:
import java.util.Objects;
public class CustomString {
private String value;
public CustomString(String value) {
this.value = value;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
CustomString that = (CustomString) obj;
return Objects.equals(value, that.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
public static void main(String[] args) {
CustomString str1 = new CustomString("Hello");
CustomString str2 = new CustomString("Hello");
System.out.println(str1.equals(str2)); // Output: true
System.out.println(str1.hashCode() == str2.hashCode()); // Output: true
}
}
In this example, the equals()
method compares the value
field of two CustomString
objects, and the hashCode()
method returns the hash code of the value
field.
8.6 Potential Issues with Hashing
- Collisions: Hash collisions occur when two different strings have the same hash code. This can degrade performance in data structures like
HashMap
andHashSet
, as theequals()
method must be used to compare the colliding elements. - Poor Hash Function: A poor hash function can result in many collisions, leading to poor performance. Java’s
String.hashCode()
method is generally well-behaved, but custom hash functions should be carefully designed to minimize collisions.
This image illustrates how string hashing works in Java, where each string is assigned a unique hash code for efficient comparison in data structures.
9. How Can Locale Affect String Comparison In Java?
Locale in Java represents a specific geographical, political, or cultural region. It can affect string comparison by influencing the way characters are interpreted and compared, especially when dealing with accented characters, case conversions, and sorting.
9.1 Understanding Locale
A Locale
object represents a specific language or region. It can affect various aspects of string processing, including:
- Case Conversion: The rules for converting characters to uppercase or lowercase can vary between locales.
- Sorting: The order in which characters are sorted can vary between locales.
- Character Interpretation: The interpretation of certain characters can vary between locales.
9.2 Using Collator
for Locale-Sensitive String Comparison
The Collator
class in Java provides locale-sensitive string comparison. It allows you to compare strings according to the rules of a specific locale.
Example:
import java.text.Collator;
import java.util.Locale;
public class StringComparison {
public static void main(String[] args) {
String str1 = "cote";
String str2 = "côte";
// Get a Collator for the French locale
Collator collator = Collator.getInstance(Locale.FRENCH);
// Compare the strings using the Collator
int result = collator.compare(str1, str2);
if (result < 0) {
System.out.println(str1 + " is less than " + str2);
} else if (result > 0) {
System.out.println(str1 + " is greater than " + str2);
} else {
System.out.println(str1 + " is equal to " + str2); // Output: cote is less than côte
}
}
}
In this example, the Collator
for the French locale is used to compare the strings "cote"
and "côte"
. The result indicates that "cote"
is less than "côte"
according to the French sorting rules.
9.3 Case-Insensitive Locale-Sensitive Comparison
You can also perform case-insensitive locale-sensitive string comparison by setting the strength of the Collator
to Collator.PRIMARY
.
Example:
import java.text.Collator;
import java.util.Locale;
public class StringComparison {
public static void main(String[] args) {
String str1 = "cote";
String str2 = "CÔTE";
// Get a Collator for the French locale
Collator collator = Collator.getInstance(Locale.FRENCH);
// Set the strength to PRIMARY for case-insensitive comparison
collator.setStrength(Collator.PRIMARY);
// Compare the strings using the Collator
int result = collator.compare(str1, str2);
if (result < 0) {
System.out.println(str1 + " is less than " + str2);
} else if (result > 0) {
System.out.println(str1 + " is greater than " + str2);
} else {
System.out.println(str1 + " is equal to " + str2); // Output: cote is equal to CÔTE
}
}
}
In this example, the Collator
is set to Collator.PRIMARY
, which ignores case differences during the comparison.
9.4 Practical Applications of Locale-Sensitive String Comparison
Locale-sensitive string comparison is useful in various scenarios, such as:
- Sorting Names: Sorting a list of names according to the rules of a specific locale.
- Searching: Searching for text in a document or database, taking into account the locale-specific interpretation of characters.
- Data Validation: Validating input according to the rules of a specific locale.
9.5 Choosing the Right Locale
When performing locale-sensitive string comparison, it is important to choose the correct locale. The locale should match the language or region of the data being compared.
This image illustrates how locale affects string comparison by showing different sorting orders based on different regional settings.
10. What Are Some Common Pitfalls To Avoid When Comparing Strings In Java?
When comparing strings in Java, several common pitfalls can lead to unexpected behavior or errors. Avoiding these pitfalls is essential for writing robust and reliable code.
10.1 Using ==
Instead of equals()
As mentioned earlier, using the ==
operator to compare strings can lead to incorrect results because it compares the references of the string objects rather than their content. Always use the equals()
method to compare the content of strings.
10.2 Ignoring Case Sensitivity
Forgetting to account for case sensitivity can lead to incorrect comparisons. Use the equalsIgnoreCase()
method when