Skip to main content
๐Ÿ“œ WAYPOINT LESSON

The API's edge behavior

โญโญ intermediateโณ 12 min read๐Ÿ“ Lesson 97 of 256

substring exclusivity, legal empty substrings, indexOf conventions, compareTo.

Everything on the exam uses the Java Quick Reference String subset: length, substring (1- and 2-arg), charAt, indexOf, equals, compareTo. Mastery means knowing their edge behavior, not their definitions.

substring's end is exclusive. "abcdef".substring(2, 5) is "cde" โ€” indexes 2, 3, 4. The length of substring(a, b) is exactly b - a. That single fact answers a whole family of exam questions.

The empty case. "".substring(0) is ""; s.substring(s.length()) is "" (legal!); but s.substring(s.length() + 1) throws StringIndexOutOfBoundsException. The legal-but-surprising s.substring(s.length()) returning "" is a favorite MCQ answer.

indexOf returns -1 when absent, 0 when prefix. "abc".indexOf("bc") is 1; "abc".indexOf("d") is -1; "abc".indexOf("") is 0. And s.indexOf(x) searching again after a hit needs a start index โ€” the 1-arg form always returns the first.

compareTo decides order. Negative โ†’ s comes first; 0 โ†’ equal content; positive โ†’ s comes second. Used for alphabetical ordering ("which string is earlier?") โ€” never for equality (== compares references and is wrong even when it sometimes looks right).