Monday, October 6, 2025
HomeLanguagesJavaCharacter.equals() method in Java with examples

Character.equals() method in Java with examples

The java.lang.Character.equals() is a function in Java which compares this object against the specified object. If the argument is not null then the result is true and is a Character object that represents the same char value as this object.

Syntax:

public boolean equals(Object obj)

Parameters: The function accepts a single parameter obj which specifies the object to be compared with.

Return Value: The function returns a boolean value. It returns true if the objects are same, otherwise, it returns false.

Below programs illustrates the above method:

Program 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);
    }
}


Output:

Z and Z are equal is true

Program 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);
    }
}


Output:

a and A are equal is false

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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32338 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6707 POSTS0 COMMENTS
Nicole Veronica
11871 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6825 POSTS0 COMMENTS
Ted Musemwa
7089 POSTS0 COMMENTS
Thapelo Manthata
6779 POSTS0 COMMENTS
Umr Jansen
6779 POSTS0 COMMENTS