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


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


java.lang.Character.equals()是Java中的一個函數,用於將該對象與指定對象進行比較。如果參數不為null,則結果為true,並且是一個Character對象,表示與此對象相同的char值。

用法:

public boolean equals(Object obj)

參數:該函數接受單個參數obj,該參數指定要與之進行比較的對象。


返回值:該函數返回一個布爾值。如果對象相同,則返回true,否則返回false。

以下示例程序旨在說明上述方法:

示例1:

// Java program to demonstrate the function 
// Character.equals() when two objects are same 
import java.lang.*; 
  
public class gfg { 
  
    public static void main(String[] args) 
    { 
  
        // assign values to c1, c2 
        Character c1 = new Character('Z'); 
        Character c2 = new Character('Z'); 
  
        // assign the result of equals method on c1, c2 to res 
        boolean res = c1.equals(c2); 
  
        // print res value 
        System.out.println(c1 + " and " + c2 + " are equal is " + res); 
    } 
}
輸出:
Z and Z are equal is true

示例2:

// Java program to demonstrate function 
// when two objects are different 
  
import java.lang.*; 
  
public class gfg { 
  
    public static void main(String[] args) 
    { 
  
        // assign values to c1, c2 
        Character c1 = new Character('a'); 
        Character c2 = new Character('A'); 
  
        // assign the result of equals 
        // method on c1, c2 to res 
        boolean res = c1.equals(c2); 
  
        // prints the res value 
        System.out.println(c1 + " and " + c2 + " are equal is " + res); 
    } 
}
輸出:
a and A are equal is false

參考: https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#equals(java.lang.Object)



相關用法


注:本文由純淨天空篩選整理自Twinkl Bajaj大神的英文原創作品 Character.equals() method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。