Java Character equals()方法及示例
java.lang.Character.equals() 是Java中的一个函数,它将此对象与指定对象进行比较。如果参数不为空,那么结果为真,是一个代表与此对象相同的char值的Character对象。
语法
public boolean equals(Object obj)
参数: 该函数接受一个单一的参数obj,指定要比较的对象。
返回值: 该函数返回一个布尔值。如果对象相同,则返回真,否则,返回假。
下面的程序说明了上述方法。
程序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)