字符類的 equals(Object obj) 方法將對象與指定的對象進行比較。當且僅當參數不為 null 並且 Character 對象具有與對象所表示的相同的 char 值時,結果才為真。
用法
public boolean equals(Object obj)
參數
上述方法隻需要一個參數:
需要比較的對象。
返回值
如果對象相同,則 equals(Object obj) 方法返回 true。否則,該方法返回 false。
例子1
public class JavaCharcterEqualsExample1 {
public static void main(String[] args) {
//Create a Character
Character charValue = new Character('A');
// Create an Object
Object obj = new Character('A');
// Test the equality for two values
if (charValue.equals(obj)) {
System.err.println("They are equal.");
}
else {
System.err.println("They are not equal.");
}
}
}
輸出:
They are equal.
例子2
public class JavaCharacterEqualsExample2 {
public static void main(String[] args) {
//Create a Character
Character charValue = new Character('B');
// Create an Object
Object obj1 = new Character('C');
// Test the equality for two values
if (charValue.equals(obj1)) {
System.err.println("They are equal.");
}
else {
System.err.println("They are not equal.");
}
}
}
輸出:
They are not equal.
例子3
public class JavaCharacterEqualsExample3 {
public static void main(String[] args) {
//Create a Character
Character charValue = new Character('1');
// Create an Object
Object obj = new Character('1');
// Test the equality for two values
if (charValue.equals(obj)) {
System.err.println("The numbers are equal.");
}
else {
System.err.println("The numbers are not equal.");
}
}
}
輸出:
The numbers are equal.
示例 4
public class JavaCharacterEqualsExample4 {
public static void main(String[] args) {
//Create a Character
Character charValue = new Character('2');
// Create an Object
Object obj = new Character('3');
// Test the equality for two values
if (charValue.equals(obj)) {
System.err.println("The numbers are equal.");
}
else {
System.err.println("The numbers are not equal.");
}
}
}
輸出:
The numbers are not equal.
相關用法
- Java Character isLetter()用法及代碼示例
- Java Character isAlphabetic()用法及代碼示例
- Java Character isValidCodePoint()用法及代碼示例
- Java Character codePointCount()用法及代碼示例
- Java Character isISOControl()用法及代碼示例
- Java Character getType()用法及代碼示例
- Java Character getNumericValue()用法及代碼示例
- Java Character isSpace()用法及代碼示例
- Java Character toTitleCase()用法及代碼示例
- Java Character isMirrored()用法及代碼示例
- Java Character isBmpCodePoint()用法及代碼示例
- Java Character toUpperCase()用法及代碼示例
- Java Character isIdentifierIgnorable()用法及代碼示例
- Java Character isDigit()用法及代碼示例
- Java Character digit()用法及代碼示例
- Java Character toChars()用法及代碼示例
- Java Character compare()用法及代碼示例
- Java Character isLowerCase()用法及代碼示例
- Java Character isJavaIdentifierPart()用法及代碼示例
- Java Character getType(char ch)用法及代碼示例
注:本文由純淨天空篩選整理自 Java Character equals() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。