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

*Demo won’t work in incognito window

Sign Up for Our Newsletters

Get only the best articles delivered to your mail

You May Also Like

How to clear Java cache?

Table of Contents Hide Open Java control panelClear Java CacheRelated Posts: You should know how to clear java cache because temporary java files can impact the browser’s performance as well…
View Post

java.lang.NullPointerException

Java throws the exception java.lang.NullPointerException, when you declare an object but do not instantiate it and yet use it for calling a class method or setting / getting variables. Consider…
View Post
break text file into columns java

Break text file into columns Java

Table of Contents Hide IntroductionGetting StartedUsing String.split()Using StringTokenizerUsing Pattern.compile ()Code ExampleResultsLive DemoRelated Posts: Introduction To break text file into columns in Java you can use the split() method. There are…
View Post

How to end program in Java?

In order to know how to end program in Java, you should be aware that calling return with in a function doesn’t end it. You need to use System.exit(0) command…
View Post