How to Compare Characters in Java Programming

How to Compare Characters in Java Programming thumbnail
Java programs can compare text characters.

As a Java developer, you can compare characters within your programs. Java stores characters in a way that allows you to compare them easily. Characters and integers are stored in similar ways in Java, so characters are compared using their numerical values. In most cases, character comparison involves figuring out which character occurs first alphabetically, in which case it is considered lower. Comparing characters can be a useful function when organizing text Strings if you plan to store them in alphabetical order.

Instructions

    • 1

      Prepare two characters in your Java program. You can either create primitive character type variables directly or use the characters occurring in any text Strings you have. The following example code illustrates both methods:

      char firstChar = 'g';

      String fullString = "apple";

      char secondChar = fullString.charAt(1);//'p'

      The code creates two character variables. Both are lower-case alphabetical letters, "g" and "p", respectively.

    • 2

      Compare your characters. The following sample code demonstrates comparing the two characters to determine which is first alphabetically:

      if(firstChar<secondChar) System.out.println("First character comes first");

      else if(secondChar<firstChar) System.out.println("Second character comes first");

      else System.out.println("Both characters are equal");

      The comparison works the same way as comparing two numbers, either one character is greater or they are equal. Save your code, compile and run it to see the output. Experiment by altering the character variables to check that the comparison works accurately.

    • 3

      Compare characters with different cases. Upper and lower case characters are represented in Java using different values. For this reason, your comparisons may sometimes have unexpected results. Alter the first line of your code as follows to demonstrate the difference:

      char firstChar = 'S';

      Run your program, it should output the following:

      First character comes first

      This may be unexpected as the letter "S" is later alphabetically than the letter "p." Java represents primitive type characters using their ASCII values, with upper case letters represented using lower numbers. Bear this issue in mind when you are comparing characters.

    • 4

      Use Character objects to represent your characters. The Java language provides the Character class to model characters as objects rather than primitive types. Use these objects in your code as follows:

      Character firstCharacter = new Character('v');

      Character secondCharacter = new Character('t');

      The Character class is a wrapper class for the primitive type "char." Character objects are instantiated by passing the character represented in its primitive type form as a parameter to the class constructor as in this example.

    • 5

      Compare your Character objects. The Character class provides a comparison method as follows:

      int result = firstCharacter.compareTo(secondCharacter);

      if(result<0) System.out.println("First Character comes first");

      else if(result>0) System.out.println("Second Character comes first");

      else System.out.println("Both characters are equal");

      Enter this code and test your program again, it should output the following:

      Second Character comes first

      Note that the upper case characters are still represented as lower in value when you use the Character class.

Tips & Warnings

  • You can avoid the issue of upper case characters having lower values by using the String object "compareToIgnoreCase" method instead of comparing individual characters.

  • Your code may behave unpredictably if you compare non alphanumeric characters.

Related Searches:

References

Resources

  • Photo Credit BananaStock/BananaStock/Getty Images

Comments

You May Also Like

Related Ads

Featured