當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Character equals()用法及代碼示例


字符類的 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 equals() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。