JIYIK CN >

Current Location:Home > Learning > PROGRAM > Java >

How to compare characters for equality in Java

Author:JIYIK Last Updated:2025/04/14 Views:

This tutorial shows you how to check if two characters are equal in Java.

In Java, we can use the equals( ==) operator or the Charactercompare() equals()method of the Character class to compare two characters. If you are working with primitive character values, you can simply use ==the equality operator, but with character class instances, use the compare( equals()) method.

In this article, we will learn the use of these two comparison equality methods through examples. Let’s get started.


Checking for Equality of Characters in Java Using ==Equality Operator

Java uses ==the equality operator to check whether two values ​​are equal or not. We can use this operator to check whether two characters are equal or not.

In this example, we created three characters and compared them using the == equality operator. This operator returns true if two characters are equal, otherwise it returns false.

public class SimpleTesting {
  public static void main(String[] args) {
    char ch1 = 'J';
    char ch2 = 'K';
    char ch3 = 'J';
    System.out.println(ch1 == ch2);
    System.out.println(ch2 == ch3);
    System.out.println(ch1 == ch3);
  }
}

Output:

false
false
true

equals()Check for equal characters using the method in Java

If you are using Characterthe class and want to compare two character values, then use the compare method belonging to Objectthe class equals(), which returns true if the objects are equal, otherwise it returns false. See the example below.

public class SimpleTesting {
  public static void main(String[] args) {
    Character ch1 = 'J';
    Character ch2 = 'K';
    Character ch3 = 'J';
    System.out.println(ch1.equals(ch2));
    System.out.println(ch2.equals(ch3));
    System.out.println(ch1.equals(ch3));
  }
}

Output:

false
false
true

compare()Check for equal characters using the method in Java

Here is another solution that can be used to check if two characters are equal. compare()The method belongs to Stringthe class and returns 0 if the two values ​​are equal.

Here, we use this method and ==the equality operator to verify if it returns 0. If it returns 0, then both values ​​are equal. See the example below.

public class SimpleTesting {
  public static void main(String[] args) {
    Character ch1 = 'J';
    Character ch2 = 'K';
    Character ch3 = 'J';
    System.out.println(Character.compare(ch1, ch2) == 0);
    System.out.println(Character.compare(ch2, ch3) == 0);
    System.out.println(Character.compare(ch1, ch3) == 0);
  }
}

Output:

false
false
true

When checking equality of two objects, always check the value. Java does not consider lowercase and uppercase equal. We consider the two values ​​to be the same, but Java works on Unicode values ​​and the two variables hold different Unicode. That is why Java returns false to the console. See the code example and understand how Java treats lowercase and uppercase characters differently.

public class SimpleTesting {
  public static void main(String[] args) {
    Character ch1 = 'J';
    Character ch2 = 'j';
    System.out.println(ch1 == ch2);
  }
}

Output:

false

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Compile multiple Java files with a single command in Java

Publish Date:2025/04/14 Views:192 Category:Java

This tutorial explains how to compile multiple java files using a single command in Java. Compilation is a term used to refer to the process of converting java source code into bytecode using JDK. To execute any Java file, we need to follow

Arrow operator in Java ->

Publish Date:2025/04/14 Views:113 Category:Java

This tutorial explains - the role of the arrow operator ( ) in Java and lists some sample code to understand the topic. In Java 8, a new feature lambda expression was added, and the arrow operator appeared in Java to form lambda expressions

>> operator in Java

Publish Date:2025/04/14 Views:66 Category:Java

This guide will introduce you to the operator in Java . To understand this concept, you need to be familiar with some lower-level computing concepts. For example, bits, bytes, etc. Let's take a deeper look. Operators in Java In Java, the op

Store Div Id in PHP variable and pass it to JavaScript

Publish Date:2025/04/13 Views:51 Category:PHP

This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s

Sending Emails Using the Mail Form in PHP

Publish Date:2025/04/12 Views:116 Category:PHP

This article will demonstrate installing sendmail the library and sending an email through the PHP mail form. Install sendmail to send email from PHP on local server PHP has a built-in function mail() to send emails. However, this function

Plotting a Pandas Series

Publish Date:2025/04/13 Views:102 Category:Python

This article explores the concept of plotting series on a DataFrame using Pandas. Whether you are exploring a dataset to hone your skills or aiming to make a good presentation for your company’s performance analysis, visualization plays a

GroupBy Application in Pandas

Publish Date:2025/04/13 Views:177 Category:Python

This tutorial aims to explore the concepts in Pandas GroupBy Apply . Pandas is used as an advanced data analysis tool or package extension in Python. When we have data in SQL tables, spreadsheets, or heterogeneous columns, Pandas is highly

Flattening Hierarchical Indexes in Pandas Columns

Publish Date:2025/04/12 Views:183 Category:Python

This article will discuss how to flatten a hierarchical index in a Pandas Dataframe column. Groupby aggregation functions are often used to create hierarchical indexes. The used aggregation function will be visible in the hierarchical index

DATETIME vs. TIMESTAMP in MySQL

Publish Date:2025/04/11 Views:117 Category:MySQL

DATETIME and TIMESTAMP are two different data types that can be used to store values ​​that must contain both a date and a time portion. In this article, we will understand how it is stored in the database and the memory required for ea

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial