How to compare two characters in Java


JavaViews 6960

How to compare two characters in Java

This tutorial will help you understand various methods on how to compare two characters in Java along with detailed examples. Java supports various built-in methods like compare() and equals() methods to compare various characters. We can compare both primitive characters and Character objects. Let us see each method in detail below.

Compare characters in Java

Comparing primitive characters

We can compare primitive characters by using either the compare() method or by using relational operators like <, > or = operators.

Using compare() method

The compare() method belongs to the Character class and compares two characters numerically. Below is the syntax of the compare() method.

public static int compare(char x, char y);

Parameters: This method accepts two characters that need to be compared.

Return value: This method returns either of the below values as a result of the comparison.

  • 0 if both characters are equal
  • negative value(a number less than 0) if 1st character is less than second character i.e x<y
  • positive value(number greater than 0) if 1st character is greater than second character i.e x>y

Example:

The below example shows how to compare two characters in Java using the compare() method. Here the character f is less than character r. hence the method returns a negative value.

public class CompareChar {

  public static void main(String[] args) {
    char a = 'f';
    char b = 'r';
    
    int x = Character.compare(a, b);
    
    if(x>0)
      System.out.println(a + " is greater than " + b);
    else if(x<0)
      System.out.println(a + " is lesser than " + b);
    else
      System.out.println(a + " and " + b + " are equal");
  }

}
f is lesser than r

Using relational operators

We can use relational operators like <, > or = to compare characters in Java. But we can use this to compare only primitive characters. The below example shows how to use the relational operators to compare two characters in Java. This is the simplest method since it doe not require any class or method.

public class CompareChar {

  public static void main(String[] args) {
    char a = 's';
    char b = 'g';
    
    if(a<b)
      System.out.println(a + " is lesser than " + b);
    else if(a>b)
      System.out.println(a + " is greater than " + b);
    else 
      System.out.println(a + " and " + b + " are equal");
    
  }

}
s is greater than g

Below is another example that compares two characters whose value is the same.

public class CompareChar {

  public static void main(String[] args) {
    char a = 's';
    char b = 's';
    
    if(a<b)
      System.out.println(a + " is lesser than " + b);
    else if(a>b)
      System.out.println(a + " is greater than " + b);
    else 
      System.out.println("Both characters are equal");
    
  }

}
Both characters are equal

Compare Characters objects

We can compare Character objects by using either the compare() method or the equals() method.

Using compare() method

Similar to using the compare() method in primitive characters, we can use the same method to compare Character objects as well.

The below example shows how to compare two characters using the compare() method.

Example:

In this example, the 1st character is lesser than the second character. Hence the compare() method returns a negative value.

public class CompareCharObjects {

  public static void main(String[] args) {
    Character c1 = 'd';
    Character c2 = 'j';
    
    int x = Character.compare(c1, c2);
    
    if(x>0)
      System.out.println(c1 + " is greater than " + c2);
    else if(x<0)
      System.out.println(c1 + " is lesser than " + c2);
    else
      System.out.println(c1 + " and " + c2 + " are equal");

  }

}
d is lesser than j

Using equals() method

The equals() method of the Character class just compares and checks if both characters are equal. If equal, it returns true, else it returns false.

In the below example, since both characters are not the same, the equals() method returns false.

public class CompareCharObjects {

  public static void main(String[] args) {
    Character c1 = 'd';
    Character c2 = 'j';
    
    if(c1.equals(c2))
      System.out.println("Both characters are equal");
    else
      System.out.println("Both characters are not equal");
    

  }

}

[indeed-social-locker sm_list=’fb,tw,li’ sm_template=’ism_template_1′ sm_list_align=’horizontal’ sm_display_counts=’false’ sm_display_full_name=’true’ unlock_type=1 locker_template=2 sm_d_text=’

This content is locked

Share This Page To Unlock The Content!

‘ enable_timeout_lk=1 sm_timeout_locker=30 ism_overlock=’default’ ]

Both characters are not equal

The below example shows how the equals() method returns true when both characters are the same.

public class CompareCharObjects {

  public static void main(String[] args) {
    Character c1 = 'd';
    Character c2 = 'd';
    
    if(c1.equals(c2))
      System.out.println("Both characters are equal");
    else
      System.out.println("Both characters are not equal");

  }

}

[/indeed-social-locker]

Both characters are equal

Reference

 

Translate »