当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。