dev only

Strings

Strings Are Objects

String is a class, part of java.lang (no import needed). Like any class, you can create one with new, or with the shorter string literal syntax:

String autoName = new String("BlueAlliance-Left"); // rarely used
String autoName = "BlueAlliance-Left";              // the normal way

Both create a String object; the literal form is what you'll use almost always.

Strings Are Immutable

Once a String is created, its characters can never change. Every method that looks like it "modifies" a string actually returns a brand-new one, leaving the original untouched:

String name = "Titan";
name.toUpperCase(); // does nothing useful — the result is thrown away!
name = name.toUpperCase(); // this is how you actually keep the change

Concatenation

+ and += join strings together, and automatically convert non-string values into strings:

String status = "Auto: " + autoName + ", step " + 3; // the int 3 converts automatically

Watch the order when mixing + with numbers — it's evaluated left to right, so "Score: " + 1 + 2 gives "Score: 12" (each number appends as text in turn), not "Score: 3". Use parentheses — "Score: " + (1 + 2) — to force the addition first.

Index and Length

Each character has a position, or index, starting at 0. length() returns the total character count — the last valid index is always length() - 1. There's no bracket notation like an array's (autoName[0] isn't valid Java) — getting a single character means using substring, covered next.

Accessing an index outside 0 to length() - 1 throws an IndexOutOfBoundsException.

The Core String Methods

String autoName = "BlueAlliance-Left";
autoName.length();          // 17
autoName.substring(0, 4);   // "Blue" — from index 0 up to (not including) 4
autoName.substring(13);     // "Left" — from index 13 to the end
autoName.indexOf("Left");   // 13 — or -1 if not found
autoName.equals("BlueAlliance-Left"); // true — compares actual characters

substring(i, i + 1) is the standard way to get just the single character at index i.

.equals(), Not ==

== on a String checks whether two variables point at the same object — not whether their text matches. Use .equals() to compare actual content:

String a = new String("Left");
String b = new String("Left");
System.out.println(a == b);       // false — two different objects
System.out.println(a.equals(b));  // true — same characters

Common Pitfalls

  • Using == to compare string content. It compiles, runs, and gives the wrong answer far more often than it errors — always use .equals() for strings.
  • Forgetting a string method's result has to be used. autoName.toUpperCase(); alone does nothing — strings are immutable, so the result must be captured (autoName = autoName.toUpperCase();) or it's lost.
  • Off-by-one in substring. The to index in substring(from, to) is exclusive — substring(0, 4) gives 4 characters (indices 0-3), not 5.

Key Takeaways

  • String is a class; string literals ("text") are the normal way to create one.
  • Strings are immutable — every "modifying" method returns a new string instead of changing the original.
  • +/+= concatenate, auto-converting non-string values to text.
  • length(), substring(), indexOf() are the core methods, all indexed from 0.
  • Use .equals() to compare string content — == only checks whether two variables reference the same object.

Derived from csawesome-2026: unit-1-using-objects-and-methods/1.15-strings.md (CSA 1.15)