Java Character Comparison and Empirical Testing, CS 124 – Study Notes
offline

Difficulty: Beginner | Prerequisites: Basic Java syntax, familiarity with System.out.println.

Tags: char comparison Java, ASCII, Unicode, character ordering, < > operators on char, empirical testing, CS 124 UIUC, AP CSA, boolean expressions, primitive types


Big Picture

In Java, the char type is not a letter; it is a number. Every character maps to an integer value via the Unicode standard (which includes ASCII as its first 128 entries). Because characters are numbers under the surface, you can compare them with <, >, <=, >=, ==, and !=, exactly like int values. This topic sits at the intersection of data representation and good debugging habits: knowing that characters are numeric lets you reason about sorting and comparison, and knowing how to verify behaviour with a one-line program is a transferable skill for the rest of the course and beyond.


TL;DR

Java char values are integers (Unicode code points), so you can compare them with relational operators. When you are unsure about character ordering, the right move is to test it with a short program, not to guess from letter shapes or memorise an entire ASCII table.


Key Terms

char (primitive type)

A 16-bit unsigned integer type in Java that stores a single Unicode character. Think of it as: a number that Java displays as a letter when you print it.

ASCII (American Standard Code for Information Interchange)

A character encoding that assigns integer values 0 through 127 to common characters. The lowercase letters 'a' through 'z' occupy codes 97 through 122; uppercase 'A' through 'Z' occupy 65 through 90; digits '0' through '9' occupy 48 through 57. In simple terms, it is the agreed-upon numbering scheme for the characters on a US keyboard.

Unicode

A superset of ASCII that covers characters from virtually every writing system. Java's char type uses Unicode, which means the first 128 values are identical to ASCII, but the full range extends to 65,535 for char (and beyond with supplementary characters).

Relational operators on char

The operators <, >, <=, >=, ==, != work on char values by comparing their underlying integer codes. 'a' < 'm' evaluates to true because 97 < 109.


Core Content

Characters are numbers

Every char literal in Java corresponds to an integer:

  • 'a' = 97, 'b' = 98, ..., 'z' = 122

  • 'A' = 65, 'B' = 66, ..., 'Z' = 90

  • '0' = 48, '1' = 49, ..., '9' = 57

You do not need to memorise these values. What matters is the ordering:

  • Digits come before uppercase letters

  • Uppercase letters come before lowercase letters

  • Within each group, the order matches the alphabet

Comparing characters with relational operators

Because char is a numeric type, comparisons work directly:

System.out.println('a' < 'm');   // true, because 97 < 109
System.out.println('A' < 'a');   // true, because 65 < 97
System.out.println('5' < 'A');   // true, because 53 < 65

These return boolean values and can be used in if statements, loops, and any other context that expects a boolean expression.

The right approach: test it with code

When a question asks "how would you determine whether one character comes before another?", the correct mindset is:

  • Write a one-line program: System.out.println('a' < 'm');

  • Run it, observe the output

  • Generalise from the result

This is more reliable than guessing from letter shapes (letter shape has nothing to do with numeric ordering) and more practical than memorising the full ASCII table (unnecessary and not what introductory CS courses expect). The principle is broader than characters: when you are unsure how a language feature behaves, write the smallest possible experiment and run it.

Why the wrong answers are wrong

  • "Guess based on letter shapes" confuses visual form with numeric encoding. The shape of the letter a tells you nothing about whether its code point is higher or lower than m.

  • "Characters cannot be compared in Java" is factually incorrect. char is a primitive numeric type and supports all relational operators.

  • "Memorise all ASCII character codes" is not the skill being tested. Knowing the general ordering (digits < uppercase < lowercase, alphabetical within each group) is sufficient. The exam is testing whether you know how to verify, not whether you have memorised a lookup table.


Real-World Applications

Character comparison is the foundation of lexicographic sorting, which is how Java's String.compareTo() method works internally: it walks through characters and compares their numeric values. Every time you sort a list of strings alphabetically, this mechanism is at play underneath.


Common Misconceptions

  • "You cannot use < or > on characters in Java." You can. char is a numeric primitive, and relational operators work on it just as they do on int.

  • "Lowercase and uppercase letters have the same value." They do not. 'a' is 97, 'A' is 65. Case matters in character comparison.

  • "The alphabet goes A, a, B, b, C, c, ... in Java." It does not. All uppercase letters (65 to 90) come before all lowercase letters (97 to 122). The groups are separate, not interleaved.

  • "You need to memorise ASCII codes to answer comparison questions." You need to know the general order (digits, then uppercase, then lowercase) and that alphabetical order holds within each group. For anything else, test it with code.


Why It Matters / Exam Flags

⚠️ CS 124 uses character-comparison questions to test your problem-solving mindset, not your memorisation. Expect questions that ask which approach (test, guess, memorise) is correct.

⚠️ Mixed-case comparisons ('A' < 'a') are a common exam trap. Remember: all uppercase letters have lower codes than all lowercase letters.

⚠️ If you see char in an arithmetic expression (e.g. 'a' + 1), Java promotes it to int. The result of 'a' + 1 is the int 98, not the char 'b', unless you cast it back: (char)('a' + 1).


Quick Self-Test

True or false: 'z' > 'a' evaluates to true in Java.

A: True. 122 > 97.

True or false: 'A' > 'a' evaluates to true in Java.

A: False. 65 < 97, so 'A' is less than 'a'.

Fill in the blank: The best way to check whether 'd' < 'q' in Java is to ______.

A: Write and run a one-line test program, such as System.out.println('d' < 'q');.


Practice Q&A

Q: What does System.out.println('a' < 'm'); print?

A: true. The Unicode value of 'a' is 97 and 'm' is 109, so 97 < 109 is true.

Q: A student claims that characters cannot be compared with < in Java. What is wrong with this claim?

A: char is a 16-bit unsigned integer type in Java. All relational operators (<, >, <=, >=, ==, !=) work on char values by comparing their underlying numeric codes.

Q: Without looking up an ASCII table, how should you determine whether 'G' comes before 'g' in Java's character ordering?

A: Write a minimal test: System.out.println('G' < 'g');. Running it will print true, confirming that uppercase letters have lower code values than lowercase letters.

Q: What is the result of the expression 'B' - 'A' in Java, and what type is it?

A: The result is 1, and its type is int. Java promotes both char operands to int before performing the subtraction.


Connections to Other Topics

This connects to Java's type promotion rules: when char appears in an arithmetic expression, it is promoted to int, which is the same widening mechanism covered in the compound assignment operators topic. It also connects to String comparison via compareTo() and equals(), which you will encounter when working with objects. The empirical-testing mindset ("write the smallest experiment") carries into every debugging and exploration task in the course.


Related Terms / Search Tags

char comparison, character ordering Java, ASCII table, Unicode, relational operators, < > on char, boolean expression, type promotion char to int, lexicographic order, String compareTo, empirical testing, CS 124, CS124 UIUC, AP CSA, Intro to Computer Science, Java char type, character arithmetic