How to compare chars in Java?

Total
0
Shares

If you are wondering how to compare chars in java, then this article is for you. There are various ways to compare characters in Java language. We can either define the characters using char datatype or we can use Character class. Both are equally good and acceptable.

Compare chars, if defined using char datatype

Defined like this – char firstChar = 'A';

a. Using ==

public class Main {
  public static void main(String[] args) {
    char firstChar = 'H';
    char secondChar = 'E';
    char thirdChar = 'H';
    
    System.out.println(firstChar == thirdChar);
    // Output True
    
    System.out.println(secondChar == thirdChar);
    // Output False
  }
}

    Tweet this to help others

b. Using Character.compare(char1, char2)

There are 3 possible outputs (if provided parameters are characters) –

  • 0 – If both characters are equal.
  • Negative number – If char1 is smaller than char2. For ex – char1 = A and char2 = B.
  • Positive number – If char1 is larger than char2.
public class Main {
  public static void main(String[] args) {
    char firstChar = 'H';
    char secondChar = 'E';
    char thirdChar = 'H';
    
    System.out.println(Character.compare(firstChar, secondChar));
    // Output: 3 (Positive because H - E = 3)

  }
}

c. Using Character.hashCode(char)

You can calculate hasCode for your characters and then compare using if/else conditions.

public class Main {
  public static void main(String[] args) {
    char firstChar = 'H';
    char secondChar = 'E';
    char thirdChar = 'H';
    
    int firstCharHashCode = Character.hashCode(firstChar);

    int secondCharHashCode = Character.hashCode(secondChar);
    int thirdCharHashCode = Character.hashCode(thirdChar);
    
    System.out.println(firstCharHashCode - secondCharHashCode);
    // Output: 3 (positive - first char > second char)

    System.out.println(firstCharHashCode - thirdCharHashCode);
    // Output: 0 (first char is same as third char)
    
  }
}

If defined using Character class

Defined like this – Character firstCharObj = new Character('A');

a. Using compareTo()

Its syntax is firstCharObj.compareTo(secondCharObj)

public class Main {
  public static void main(String[] args) {
    Character firstCharObj = new Character('H');
    Character secondCharObj = new Character('E');
    Character thirdCharObj = new Character('H');
    
    System.out.println(firstCharObj.compareTo(secondCharObj));
    // Output: positive, negative or 0
    
  }
}

b. Using equals()

This will check if two characters are equal or not.

public class Main {
  public static void main(String[] args) {
    Character firstCharObj = new Character('H');
    Character secondCharObj = new Character('E');
    Character thirdCharObj = new Character('H');
    
    System.out.println(firstCharObj.equals(thirdCharObj));
    // True | False
    
  }

c. Using getNumericValue()

public class Main {
  public static void main(String[] args) {
    Character firstCharObj = new Character('H');
    Character secondCharObj = new Character('E');
    Character thirdCharObj = new Character('H');

	int firstCharNumericVal = Character.getNumericValue(firstCharObj);
    int secondCharNumericVal = Character.getNumericValue(secondCharObj);
	int thirdCharNumericVal = Character.getNumericValue(thirdCharObj);

    System.out.println(firstCharNumericVal - secondCharNumericVal);
    // Output: positive | negative | 0
    
  }
}

You may also use comparison characters like >, <, == while comparing numerical values. For example –

System.out.println(firstCharNumericVal > secondCharNumericVal);
// True | False

d. Using Character.hashCode(charObj)

hashCode() method works with both characters and characters objects.

public class Main {
  public static void main(String[] args) {
    Character firstCharObj = new Character('H');
    Character secondCharObj = new Character('E');
    Character thirdCharObj = new Character('H');
    
    int firstCharHashCode = Character.hashCode(firstCharObj);
    int secondCharHashCode = Character.hashCode(secondCharObj);
    int thirdCharHashCode = Character.hashCode(thirdCharObj);

    System.out.println(firstCharHashCode - secondCharHashCode);
    // Output: positive | negative | 0
    
  }
}

e. Using charObj.hashCode()

Apart from static method, there is also a public method to call on object directly.

public class Main {
  public static void main(String[] args) {
    Character firstCharObj = new Character('H');
    Character secondCharObj = new Character('E');
    Character thirdCharObj = new Character('H');
    
    int firstCharHashCode = firstCharObj.hashCode();
    int secondCharHashCode = secondCharObj.hashCode();
    int thirdCharHashCode = thirdCharObj.hashCode();

    System.out.println(firstCharHashCode - secondCharHashCode);
    // Output: positive | negative | 0
  }
}

f. Using charObj.charValue()

charValue() returns the character from object. So if your object is –

Character charObj = new Character(‘c’);

then, charObj.charValue() will return c.

public class Main {
  public static void main(String[] args) {
    Character firstCharObj = new Character('H');
    Character secondCharObj = new Character('E');
    Character thirdCharObj = new Character('H');
    
    System.out.println(firstCharObj.charValue() == thirdCharObj.charValue());
    // Output: True | False
  }
}

Merged Code Example

public class Main {
  public static void main(String[] args) {
    char firstChar = 'H';
    char secondChar = 'E';
    char thirdChar = 'H';
    
    Character firstCharObj = new Character('H');
    Character secondCharObj = new Character('E');
    Character thirdCharObj = new Character('H');
    
    System.out.println(firstChar == thirdChar);
    
    System.out.println(Character.compare(firstChar, secondChar));
    
    System.out.println(firstCharObj.compareTo(secondChar));
    
    System.out.println(firstCharObj.equals(thirdCharObj));
    
    System.out.println(Character.getNumericValue(secondCharObj));
    
    System.out.println(Character.hashCode(secondCharObj));
    
    System.out.println(Character.hashCode(secondChar));
    
    System.out.println(secondCharObj.hashCode());
    
    System.out.println(firstCharObj.charValue() == thirdCharObj.charValue());
  }
}

Live Demo

Open Live Demo