Java Character Equals Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn how to use the equals() method of Java Character class to compare Character objects and determine if they have the same value. You will also learn how to create a user-defined example that allows users to input their own values to test the code.

Set up the project

Open the terminal and create a new Java project in the ~/project directory.

mkdir ~/project
cd ~/project
mkdir JavaCharEquals
cd JavaCharEquals

Create a new Java file named CharEqualsDemo.java inside the JavaCharEquals folder.

touch CharEqualsDemo.java

Open the CharEqualsDemo.java file in a text editor.

Create and Compare Character Objects

Create three Character objects and compare them using the equals() method. Use the following code block:

public class CharEqualsDemo {
    public static void main(String[] args) {
        Character ob1 = 'a';
        Character ob2 = 'b';
        Character ob3 = 'b';
        // Comparing objects with equal and unequal values
        System.out.println("ob1 and ob2 equal?  " + ob1.equals(ob2));
        System.out.println("ob2 and ob3 equal?  " + ob2.equals(ob3));
    }
}

Save the changes and compile the code using the following command:

javac CharEqualsDemo.java

Run the program using the following command:

java CharEqualsDemo

You should see the following output:

ob1 and ob2 equal? false
ob2 and ob3 equal? true

Create User-defined Example

Create a new Java class named UserDefined inside the JavaCharEquals folder.

The program will use a Scanner object to allow the user to input two characters. The equals() method will then be used to compare the two characters and output whether they are the same or different. Use the following code:

import java.util.Scanner;

public class UserDefined {
    public static void main(String[] args) {
        try {
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter first character: ");
            Character ch1 = sc.next().charAt(0);
            System.out.print("Enter second character: ");
            Character ch2 = sc.next().charAt(0);
            boolean isEqual = ch1.equals(ch2);
            if (isEqual) {
                System.out.println("Same characters entered");
            } else {
                System.out.println("Different characters entered");
            }
        } catch (Exception e) {
            System.out.println("Invalid input! Please check.");
        }
    }
}

Save the changes and compile the code using the following command:

javac UserDefined.java

Run the program using the following command:

java UserDefined

You should see the following output:

Enter first character: m
Enter second character: m
Same characters entered

You can test the program with different values for ch1 and ch2.

Summary

Congratulations! You have completed the Java Character Equals() Method lab. You can practice more labs in LabEx to improve your skills.

Other Java Tutorials you may like