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 autoNameA = new String("BlueAlliance-Left"); // rarely used — explicit new
String autoNameB = "BlueAlliance-Left"; // the normal way — a literal
Both create a String object holding the same text; the literal form (autoNameB's style) is what you'll use almost always. (They're written here as two separately-named variables just to compare the syntax side by side — normally you'd pick one style and use it.)
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 or charAt, covered next.
Accessing an index outside 0 to length() - 1 throws a StringIndexOutOfBoundsException (a kind of IndexOutOfBoundsException).
charAt and char
substring(i, i + 1) gets a single character back as a one-character String. charAt(i) is the more direct way — it returns that one character as a char, a primitive type (Ch.2.1), not a String:
char first = autoName.charAt(0); // 'B' — a char, single-quoted
String firstAsString = autoName.substring(0, 1); // "B" — a String, double-quoted
Because char is a primitive, compare two char values with ==, not .equals() — the opposite of the rule for String:
if (autoName.charAt(0) == 'B') // correct for char — == is fine here
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.
Other Everyday String Methods
Several more String methods come up constantly in real code:
| Method | What it does |
|---|---|
equalsIgnoreCase(other) | like .equals(), but treats upper/lowercase as the same |
compareTo(other) | negative/zero/positive — alphabetical order relative to other |
contains(text) | true if text appears anywhere inside |
startsWith(text) / endsWith(text) | true if the string begins/ends with text |
isEmpty() / isBlank() | true if length is 0 / if it's empty or all whitespace |
trim() / strip() | a copy with leading/trailing whitespace removed |
replace(old, new) | a copy with every occurrence of old swapped for new |
split(regex) | breaks the string apart into a String[] around a separator |
" R2-1622 ".trim(); // "R2-1622"
"BlueAlliance".contains("Alliance"); // true
"Left".equalsIgnoreCase("LEFT"); // true
A String variable can be null — a reference (Ch.7) pointing at no object at all. Calling a method on a null String throws a NullPointerException. If you're comparing against a value that might be null, put a known-non-null literal first: "Left".equals(status) never throws even if status is null, but status.equals("Left") does.
String.format(...) (and System.out.printf(...), which prints the formatted result directly) builds a formatted string from a pattern: %.2f for a number to 2 decimal places, %d for an integer, %s for a string.
String msg = String.format("Speed: %.2f, Cycle: %d", 0.755, 3);
// "Speed: 0.76, Cycle: 3"
.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 always compiles and runs —==never errors on strings. It can even seem to work at first, since identical string literals are often the very same object — but it silently fails on strings built at runtime (substring, concatenation, input). 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. Thetoindex insubstring(from, to)is exclusive —substring(0, 4)gives 4 characters (indices 0-3), not 5.
Key Takeaways
Stringis 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(),charAt()are the core methods, all indexed from 0.charAt()returns achar(compare with==);substring()returns aString(compare with.equals()).equalsIgnoreCase,contains,trim/strip,replace,split, andString.formatcover most everyday text work; anullString throwsNullPointerExceptionif you call a method on it.- 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)