You are currently viewing How To Compare Characters in Java

How To Compare Characters in Java

This tutorial will teach you how to compare characters in java in Java using a variety of methods and examples. To compare different characters, Java provides built-in methods such as compare() and equals(). Both primitive characters and Character objects can be compared. Let’s take a closer look at each method. Although we can use the less than and greater than operators, they only work with primitive values.

To compare two char values numerically, use the compare(char x, char y) method of the Character class. The final value returned is similar to what would be returned by:

Character.valueoOf(x).compareTo(Character.valueOf(y))

Syntax

public static intcompare(char x, char y)

Comparing char primitives

  • Using <, >, ==  operators

The char primitive data type also has an associated integer value, according to the Unicode table. You should be able to compare two characters using the ==, >, and > operators in the same way that you compare two integers.

  • Note that using the < , >, or == operators to compare char primitive values returns a boolean value.

Example

public class CompareChar {
public static void main(String args[]) {
char ch1 = ‘A’;
char ch2 = ‘B’;
char ch3 = ‘A’;
System.out.println(ch1==ch2);
System.out.println(ch1<ch2);
System.out.println(ch3==ch1);
}
}

Output:

false
true
true

Using Character.compare(char x, char y)

Character can be used to compare primitive character. equals() or compare() are two methods that can be used to compare two objects.

  • Character.compare(char x, char y)

We can convert the char primitive value to a Character object by using the Character class constructor.

public class CharacterCompare {
public static void main(String args[]) {
Character ch1 = new Character(‘X’);
Character ch2 = new Character(‘Y’);
Character ch3 = new Character(‘X’);
System.out.println(Character.compare(ch2,ch3));
System.out.println(Character.compare(ch1,ch3));
System.out.println(Character.compare(ch1,ch2));
}
}

Output:

1
0
-1

Using equals() method

The equals() method determines whether two char objects are equal. If both are equal, it returns true, otherwise, it returns false.

We can also compare the Character class objects using the equals() method.

  • Note: Comparing Character class objects using equals() method returns a boolean value.

public class UsingEqual {
public static void main(String args[]) {
Character ch1 = new Character(‘X’);
Character ch2 = new Character(‘Y’);
Character ch3 = new Character(‘X’);
System.out.println(ch1.equals(ch2));
System.out.println(ch2.equals(ch3));
System.out.println(ch1.equals(ch3));
}
}

Output:

false
false
true

Example 2

public class CharacterEqual {
  
    public static void main(String[] args)
    {
  
        
        Character c1 = new Character('x');
        Character c2 = new Character('x');
  
        // the result of equals method on c1, c2 to a
        boolean a = c1.equals(c2);
  
        // print a value
        System.out.println(c1 + " and " + c2 + " are equal is " + a);
    }
}

Output:

x and x are equal is true

Example 3

public class CharacterEqual {
  
    public static void main(String[] args)
    {
  
        Character c1 = new Character('b');
        Character c2 = new Character('B');
  
        // the result of equals
        // method on c1, c2 to a
        boolean a = c1.equals(c2);
  
        // prints the a value
        System.out.println(c1 + " and " + c2 + " are equal is " + a);
    }
}

Output:

b and B are equal is false

Using relation operators

In Java, we can compare two characters using relational operators like less than and greater than. It is the simplest method and does not require the use of any classes or methods.

To compare characters in Java, we can use relational operators such as <, >, or =. However, we can only compare primitive characters with this method. In Java, the relational operators are used to compare two characters in the example below. This is the simplest method because it does not necessitate the use of any classes or methods.

public class CompareCharacter {
public static void main(String[] args){

    char a = 'A';
    char b = 'B';
    if(a > b) {
        System.out.println("A is greater");
    }else if(a < b) {
        System.out.println("A is less than B");
    }else 
        System.out.println("Both are equal");
}
}

Output

A is less than B

You may like:

Not Equal Example in Java

Java Annotations with Example

Try-with-resources in java with Example

Java enum with Example

Java enum Constructor with Example

Java Inheritance with Example | Types of inheritance

Java final Keyword with Example

Hope this article will guide you to recognize all about How To Compare Characters in Java that you needed and still if you have any problem or queries regarding this, post them in the comments section and we will be glad to assist you.

Leave a Reply