介绍
在这个实验中,你将学习如何在 Java 中有效地比较字符串。理解字符串比较对于各种编程任务来说是基础。我们将探讨标准的 equals() 方法,该方法用于检查两个字符串是否具有相同的字符序列,即使它们在内存中是不同的对象。
此外,你将学习在比较可能为 null 的字符串时,如何处理潜在的 NullPointerException 问题。最后,我们将介绍 equalsIgnoreCase() 方法,该方法允许你进行不区分大小写的字符串比较,当字符的大小写对你的比较逻辑无关紧要时,它能提供灵活性。
使用 equals() 方法比较字符串
在这一步中,你将学习如何在 Java 中使用 equals() 方法来比较字符串。比较字符串是编程中的一项基本操作,理解如何正确地进行比较至关重要。
在 Java 中,字符串是对象,而非像整数或布尔值那样的基本数据类型。这意味着当你比较字符串时,实际上是在比较对象本身,而不仅仅是它们的值。使用 == 运算符比较字符串时,检查的是两个字符串变量是否引用内存中的 同一个 对象。然而,通常你想要检查的是两个字符串是否具有 相同的字符序列,即使它们是不同的对象。为此,你应该使用 equals() 方法。
让我们创建一个简单的 Java 程序来演示这一点。
如果
HelloJava.java文件尚未在 WebIDE 编辑器中打开,请打开它。将文件的全部内容替换为以下代码:
public class StringComparison { public static void main(String[] args) { String str1 = "hello"; String str2 = "hello"; String str3 = new String("hello"); System.out.println("Comparing str1 and str2 using equals(): " + str1.equals(str2)); System.out.println("Comparing str1 and str3 using equals(): " + str1.equals(str3)); System.out.println("Comparing str1 and str2 using ==: " + (str1 == str2)); System.out.println("Comparing str1 and str3 using ==: " + (str1 == str3)); } }在这段代码中:
- 我们声明了三个字符串变量:
str1、str2和str3。 str1和str2是使用字符串字面量创建的。Java 通常会对字符串字面量进行优化,使得相同的字面量引用同一个对象。str3是使用new String()构造函数创建的,这会在内存中显式地创建一个新的字符串对象,即使其内容与现有的字面量相同。- 我们使用
equals()方法来比较字符串的内容。 - 我们使用
==运算符来比较字符串变量是否引用同一个对象。
- 我们声明了三个字符串变量:
保存文件(Ctrl+S 或 Cmd+S)。
现在,让我们编译程序。打开 WebIDE 底部的终端,并确保你位于
~/project目录中。运行以下命令:javac StringComparison.java如果编译成功,你将看不到任何输出。
~/project目录中会创建一个StringComparison.class文件。最后,使用
java命令运行编译后的程序:java StringComparison你应该会看到类似以下的输出:
Comparing str1 and str2 using equals(): true Comparing str1 and str3 using equals(): true Comparing str1 and str2 using ==: true Comparing str1 and str3 using ==: false注意,
str1.equals(str2)和str1.equals(str3)都返回true,因为字符串的内容相同。然而,str1 == str3返回false,因为str1和str3引用内存中不同的字符串对象,尽管它们包含相同的字符。在这种特定情况下,str1 == str2返回true是由于字符串字面量优化,但一般不建议依赖==来进行字符串比较,因为这可能会导致意外的结果。
这展示了为什么在 Java 中使用 equals() 方法是比较字符串内容的正确方式。
在相等性检查中处理空字符串
在这一步中,我们将探讨在 Java 中进行相等性检查时如何处理 null 字符串。null 字符串意味着该字符串变量不引用任何对象。尝试在 null 变量上调用方法会导致 NullPointerException,这是 Java 中常见的错误。
在比较字符串时,考虑到一个或两个字符串可能为 null 的情况很重要。如果你在 null 字符串上调用 equals() 方法,程序将会崩溃。
让我们修改之前的程序,看看这种情况是如何发生的,以及如何安全地处理它。
在 WebIDE 编辑器中打开
StringComparison.java文件。修改
main方法,使其包含一个null字符串:public class StringComparison { public static void main(String[] args) { String str1 = "hello"; String str2 = "hello"; String str3 = new String("hello"); String str4 = null; // This string is null System.out.println("Comparing str1 and str2 using equals(): " + str1.equals(str2)); System.out.println("Comparing str1 and str3 using equals(): " + str1.equals(str3)); System.out.println("Comparing str1 and str2 using ==: " + (str1 == str2)); System.out.println("Comparing str1 and str3 using ==: " + (str1 == str3)); // Let's try comparing with the null string // System.out.println("Comparing str1 and str4 using equals(): " + str1.equals(str4)); // This line would cause a NullPointerException // System.out.println("Comparing str4 and str1 using equals(): " + str4.equals(str1)); // This line would also cause a NullPointerException // Correct way to compare when one string might be null System.out.println("Comparing str1 and str4 safely: " + (str1 != null && str1.equals(str4))); System.out.println("Comparing str4 and str1 safely: " + (str4 != null && str4.equals(str1))); System.out.println("Comparing str4 and null safely: " + (str4 == null)); } }在修改后的代码中:
- 我们添加了一个
null字符串str4。 - 我们注释掉了如果执行会导致
NullPointerException的行。 - 我们添加了在一个字符串可能为
null时如何安全比较字符串的示例。最安全的方法是在调用equals()方法 之前,检查你要调用该方法的字符串是否不为null。常见的模式是(stringVariable != null && stringVariable.equals(anotherString))。或者,如果可能的话,你可以在已知非空的字符串上调用equals()方法,例如"hello".equals(str4)。
- 我们添加了一个
保存文件(Ctrl+S 或 Cmd+S)。
在终端中编译修改后的程序:
javac StringComparison.java运行编译后的程序:
java StringComparison你应该会看到类似以下的输出:
Comparing str1 and str2 using equals(): true Comparing str1 and str3 using equals(): true Comparing str1 and str2 using ==: true Comparing str1 and str3 using ==: false Comparing str1 and str4 safely: false Comparing str4 and str1 safely: false Comparing str4 and null safely: true输出显示,安全的比较方法能够正确处理
null字符串,而不会导致错误。str1和str4(str4为null)的比较结果正确地评估为false。
这一步强调了在 Java 中处理对象(尤其是字符串)时,处理 null 值以防止 NullPointerException 错误的重要性。
使用 equalsIgnoreCase() 进行不区分大小写的比较
在这最后一步中,我们将学习 equalsIgnoreCase() 方法,该方法用于比较字符串,而不考虑字符的大小写。当你想检查两个字符串是否相同,而不考虑字母是大写还是小写时,这个方法非常有用。
我们在前面步骤中使用的 equals() 方法进行的是区分大小写的比较。这意味着使用 equals() 时,"hello" 不等于 "Hello"。而 equalsIgnoreCase() 方法会忽略大小写,因此 "hello" 和 "Hello" 会被视为相等。
让我们最后一次修改我们的程序,来演示 equalsIgnoreCase() 方法的使用。
在 WebIDE 编辑器中打开
StringComparison.java文件。添加一些大小写不同的新字符串变量,并使用
equalsIgnoreCase()方法进行比较:public class StringComparison { public static void main(String[] args) { String str1 = "hello"; String str2 = "hello"; String str3 = new String("hello"); String str4 = null; String str5 = "Hello"; // Different casing String str6 = "HELLO"; // All uppercase System.out.println("Comparing str1 and str2 using equals(): " + str1.equals(str2)); System.out.println("Comparing str1 and str3 using equals(): " + str1.equals(str3)); System.out.println("Comparing str1 and str2 using ==: " + (str1 == str2)); System.out.println("Comparing str1 and str3 using ==: " + (str1 == str3)); // Safe comparison with null System.out.println("Comparing str1 and str4 safely: " + (str1 != null && str1.equals(str4))); System.out.println("Comparing str4 and str1 safely: " + (str4 != null && str4.equals(str1))); System.out.println("Comparing str4 and null safely: " + (str4 == null)); System.out.println("\n--- Case-Insensitive Comparisons ---"); System.out.println("Comparing str1 and str5 using equals(): " + str1.equals(str5)); System.out.println("Comparing str1 and str5 using equalsIgnoreCase(): " + str1.equalsIgnoreCase(str5)); System.out.println("Comparing str1 and str6 using equalsIgnoreCase(): " + str1.equalsIgnoreCase(str6)); System.out.println("Comparing str5 and str6 using equalsIgnoreCase(): " + str5.equalsIgnoreCase(str6)); // equalsIgnoreCase() also handles null safely if called on a non-null string System.out.println("Comparing str1 and str4 using equalsIgnoreCase(): " + str1.equalsIgnoreCase(str4)); // System.out.println("Comparing str4 and str1 using equalsIgnoreCase(): " + str4.equalsIgnoreCase(str1)); // This would still cause a NullPointerException } }在更新后的代码中:
- 我们添加了大小写不同的
str5和str6。 - 我们添加了新的打印语句,以展示
equals()和equalsIgnoreCase()方法的区别。 - 我们还展示了在非空字符串上调用
equalsIgnoreCase()方法并传入null参数不会导致错误,这与equals()方法类似。然而,在null字符串变量上调用equalsIgnoreCase()方法仍然会导致NullPointerException。
- 我们添加了大小写不同的
保存文件(Ctrl+S 或 Cmd+S)。
在终端中编译程序:
javac StringComparison.java运行编译后的程序:
java StringComparison你应该会看到类似以下的输出:
Comparing str1 and str2 using equals(): true Comparing str1 and str3 using equals(): true Comparing str1 and str2 using ==: true Comparing str1 and str3 using ==: false Comparing str1 and str4 safely: false Comparing str4 and str1 safely: false Comparing str4 and null safely: true --- Case-Insensitive Comparisons --- Comparing str1 and str5 using equals(): false Comparing str1 and str5 using equalsIgnoreCase(): true Comparing str1 and str6 using equalsIgnoreCase(): true Comparing str5 and str6 using equalsIgnoreCase(): true Comparing str1 and str4 using equalsIgnoreCase(): false输出清晰地表明,在比较字符相同但大小写不同的字符串时,
equalsIgnoreCase()方法返回true,而equals()方法返回false。
现在你已经学会了如何在 Java 中使用 equals() 方法进行区分大小写的字符串比较,如何安全地处理 null 字符串,以及如何使用 equalsIgnoreCase() 方法进行不区分大小写的字符串比较。这些都是在 Java 中处理字符串的必备技能。
总结
在这个实验中,我们学习了如何在 Java 中比较字符串。首先,我们了解了使用 == 运算符比较字符串对象和使用 equals() 方法比较字符串内容之间的区别。我们知道了 == 检查的是对象的同一性,而 equals() 检查的是字符序列是否相等。
接着,我们探讨了在比较字符串时如何处理潜在的 NullPointerException,方法是确保调用 equals() 方法的字符串不为 null。最后,我们学习了如何使用 equalsIgnoreCase() 方法进行不区分大小写的字符串比较。



