Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet52/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   48   49   50   51   52   53   54   55   ...   245
Bog'liq
Java 17 Recipes

 How It Works
One of the trickier parts of using a programming language can come when comparing 
two or more values, particularly string values. In the Java language, comparing strings 
can be fairly straightforward, keeping in mind that you should not use the == for string 
comparison. This is because the comparison operator (==) compares references, not 
values of strings. One of the most tempting things to do when programming with strings 
in Java is to use the comparison operator, but you must not because the results can vary.
Note Java uses interning of strings to speed up performance. this means 
that the JVM contains a table of interned strings, and each time the intern() 
method is called on a string, a lookup is performed on that table to find a match. 
the interning returns a canonical representation of the string. if no matching 
string resides within the table, the string is added, and a reference is returned. 
if the string already resides within the table, the reference is returned. Java 
automatically interns string literals, which can cause variations when using the == 
comparison operator.
In the solution to this recipe, you can see various techniques for comparing string 
values. The equals() method is a part of every Java object. The Java string equals() 
method has been overridden to compare the values contained within the string rather 
than the object itself. As you can see from the following examples that have been 
extracted from the solution to this recipe, the equals() method is a safe way to compare 
strings.
Chapter 3 StringS


84
// Comparison is equal
if (one.equals(var1)){
System.out.println ("String one equals var1 using equals");
}
// Comparison is NOT equal
if (one.equals(two)){
System.out.println ("String one equals two using equals");
}
The equals() method first checks whether the strings reference the same object 
using the == operator; it returns true if they do. If they do not reference the same object
equals() compare each string character by character to determine whether the strings 
being compared to each other contain the same values. What if one of the strings has 
a different case setting than another? Do they still compare equal to each other using 
equals()? The answer is no, and that is why the equalsIgnoreCase() method was 
created. Comparing two values using equalsIgnoreCase() causes each of the characters 
to be compared without paying attention to the case. The following examples have been 
extracted from the solution to this recipe.
// Comparison is NOT equal
if (two.equals(var2)){
System.out.println ("String two equals var2 using equals");
}
// Comparison is equal
if (two.equalsIgnoreCase(var2)){
System.out.println ("String two equals var2 using equalsIgnoreCase");
}
The compareTo() and compareToIgnoreCase() methods perform a lexicographical 
comparison of the strings. This comparison is based on the Unicode value of each 
character contained within the strings. The result is a negative integer if the string 
lexicographically precedes the argument string. The result is a positive integer if the 
string lexicographically follows the argument string. The result is zero if both strings 
are lexicographically equal. The following excerpt from the solution to this recipe 
demonstrates the compareTo() method.
Chapter 3 StringS


85
// Comparison is equal
if (one.compareTo(var1) == 0){
System.out.println("One is equal to var1 using compareTo()");
}
Inevitably, many applications contain code that must compare strings at some 
level. The next time you have an application requiring string comparison, consider the 
information discussed in this recipe before writing the code.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   48   49   50   51   52   53   54   55   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling