1. Overview
1.概述
In this short tutorial, we’ll explore different ways to compare characters in Java.
在这个简短的教程中,我们将探讨在Java中比较字符的不同方法。
We’ll start by discussing how to compare primitive characters. Then, we’ll look at different methods of comparing Character objects.
我们将首先讨论如何比较原始的字符。然后,我们将看一下比较字符对象的不同方法。
2. Comparing Primitive Characters
2.比较Primitive Characters
Firstly, let’s begin by highlighting how to compare primitive characters.
首先,让我们开始强调如何比较原始的字符。
2.1. Using Relational Operators
2.1.使用关系型Operators
Typically, the simplest way to compare characters is using the relational operators.
通常,比较字符的最简单方法是使用关系运算符。
In short, characters are compared in Java depending on the order of their ASCII code:
简而言之,字符在Java中的比较取决于它们的顺序ASCII代码。
assertFalse('a' == 'A');
assertTrue('a' < 'v');
assertTrue('F' > 'D');
2.2. Using Character.compare() Method
2.2.使用Character.compare()方法
Similarly, another solution would be using the compare() method of the Character class.
同样地,另一个解决方案是使用字符类的compare()方法。
Simply put, the Character class wraps a value of the primitive type char in an object. The compare() method accepts two char parameters and compares them numerically:
简单地说,Character类将一个原始类型char的值包装在一个对象中。compare()方法接受两个char参数并对它们进行数字比较。
assertTrue(Character.compare('C', 'C') == 0);
assertTrue(Character.compare('f', 'A') > 0);
assertTrue(Character.compare('Y', 'z') < 0);
As shown above, the compare(char a, char b) method returns an int value. It denotes the difference between the ASCII code of a and b.
如上所示,compare(char a, char b)方法返回一个int值。它表示a和b的ASCII码之间的差异。
The returned value is equal to zero if the two char values are identical, less than zero if a < b, and greater than zero otherwise.
如果两个char值相同,返回值等于0,如果a < b,返回值小于0,否则大于0。
3. Comparing Character Objects
3.比较字符对象
Now that we know how to compare primitive characters, let’s see how to compare Character objects.
现在我们知道了如何比较原始字符,让我们看看如何比较字符对象。
3.1. Using Character.compareTo() Method
3.1.使用Character.compareTo() 方法
The Character class provides the compareTo() method to compare two character objects numerically:
字符类提供了compareTo()方法来比较两个字符对象的数值。
Character chK = Character.valueOf('K');
assertTrue(chK.compareTo(chK) == 0);
Character chG = Character.valueOf('G');
assertTrue(chK.compareTo(chG) > 0);
Character chH = Character.valueOf('H');
assertTrue(chG.compareTo(chH) < 0);
Here, we used the valueOf() method to create Character objects since the constructor is deprecated since Java 9.
在这里,我们使用valueOf()方法来创建Character对象,因为构造函数在Java 9中被废弃。
3.2. Using Object.equals() Method
3.2.使用Object.equals() 方法
Moreover, one of the common solutions to compare objects is using the equals() method. It returns true if the two objects are equal, false otherwise.
此外,比较对象的常见解决方案之一是使用equals()方法。如果两个对象相等,它返回true,否则false。
So, let’s see how we can use it to compare characters:
因此,让我们看看如何用它来比较字符。
Character chL = 'L';
assertTrue(chL.equals(chL));
Character chV = 'V';
assertFalse(chL.equals(chV));
3.3. Using Objects.equals() Method
3.3.使用Objects.equals() 方法
The Objects class consists of utility methods for operating on objects. It offers another way to compare character objects through the equals() method:
Objects类由用于操作对象的实用方法组成。它提供了另一种通过equals()方法来比较字符对象的方法。
Character chA = 'A';
Character chB = 'B';
assertTrue(Objects.equals(chA, chA));
assertFalse(Objects.equals(chA, chB));
The equals() method returns true if the character objects are equal to each other and false otherwise.
equals()方法在字符对象彼此相等时返回true,否则返回false。
4. Conclusion
4.总结
In this article, we learned a variety of ways to compare primitive and object characters in Java.
在这篇文章中,我们学习了在Java中比较原始字符和对象字符的各种方法。
As always, the code used in this article can be found over on GitHub.
一如既往,本文中所使用的代码可以在GitHub上找到over。