All the Rules

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

Is "ox" equal to "ox" ?

Answer:

Yes. The first letters are the same, and the tails of each string are the same. In this case, x is 'o' and X is "x". y is 'o' and Y is "x". The big letters can stand for a string that is one letter long.

All the Rules

Here are the rules for string equality. The symbol x stands for a single character, as does y. The symbol X stands for a string of characters, as does Y. The symbol + stands for concatenation.

1. equals( "", "" )   = true

2. equals( "", X )    = false if X is not the empty string

3. equals( X, "" )    = false if X is not the empty string

4. equals( x+X, y+Y ) = false if x != y

5. equals( x+X, y+Y ) = true  if x == y and equals( X, Y )

Here is an example:

equals( "rat", "rat" ) = equals( "at", "at")   // rule 5
equals(  "at",  "at" ) = equals(  "t",  "t")   // rule 5
equals(   "t",   "t" ) = equals(   "",   "")   // rule 5
equals(    "",    "" ) = true                  // rule 1

For another example,

equals( "rat", "ra"  ) = equals( "at", "a")    // rule 5
equals(  "at",  "a"  ) = equals(  "t",  "")    // rule 5
equals(   "t",   ""  ) = false                 // rule 3

And yet another example,

equals( "rAt", "rat"  ) = equals( "At", "at")   // rule 5
equals(  "At",  "at"  ) = false                 // rule 4

QUESTION 14:

In the rules for string equality, which rules are the base cases?