- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - Character equals() Method
The Java Character equals() is used to compare two Character objects against each other.
The equals() method accepts a Character object as an argument, compares it with another Character object the method is invoked on, then returns a boolean value based on their equality.
The result is true if and only if the argument is not null and is a Character object that holds the same char value as this object.
Syntax
Following is the syntax of Java Character equals() method
public boolean equals(Object obj)
Parameters
obj − the object to compare with
Return Value
This method returns true if the objects are the same, false otherwise.
Example
The following example shows the usage of Java Character equals() method in case-sensitive scenarios.
package com.tutorialspoint; import java.lang.*; public class CharacterDemo { public static void main(String[] args) { // create 2 Character objects c1, c2 Character c1, c2; // create a boolean primitive res boolean res; // assign values to c1, c2 c1 = new Character('a'); c2 = new Character('A'); // assign the result of equals method on c1, c2 to res res = c1.equals(c2); // print res value System.out.println(c1 + " and " + c2 + " are equal is " + res); } }
Output
Let us compile and run the above program, this will produce the following result −
a and A are equal is false
Example
Now let us see the result when two equal character objects are compared using this method.
package com.tutorialspoint; import java.lang.*; public class CharacterDemo { public static void main(String[] args) { Character c1, c2; boolean res; c1 = new Character('3'); c2 = new Character('3'); res = c1.equals(c2); String str = c1+ " and " + c2 + " are equal is " + res; System.out.println( str ); } }
Output
If we compile and run the program above, the output is obtained as −
3 and 3 are equal is true
Example
In another case, we can also check whether two char arrays are equal or not, using this method. To do that, we make use of loop statements to traverse through the array elements and individually check whether the corresponding elements are the same.
import java.lang.*; public class CharacterDemo { public static void main(String[] args) { boolean res; int i; Character c1[] = {'a', 'e', 'i', 'o', 'u'}; Character c2[] ={'a', 'e', 'i', 'b', 'd'}; for(i = 0; i < 5; i++) { res = c1[i].equals(c2[i]); System.out.println(c1[i] + " and " + c2[i] + " are equal is " + res); } } }
Output
Once the program above is compiled and run, the output is displayed as: −
a and a are equal is true e and e are equal is true i and i are equal is true o and b are equal is false u and d are equal is false