Java String Expressions

Declaring strings
String name = "Alice"; // string literal String empty = ""; // empty string String nothing = null; // no object at all

Strings in Java are objects, not primitives. They're stored in the "String pool".

Concatenation ( + )
String first = "Hello"; String second = "World"; String msg = first + " " + second; // "Hello World" int age = 17; String s = "Age: " + age; // "Age: 17"

If one side is a String, Java converts the other side automatically.

Immutability — important concept!
String s = "hello"; s.toUpperCase(); // does NOT change s! s = s.toUpperCase(); // THIS works → "HELLO"

Strings are immutable. Every method returns a new string — you must save the result.

Escape characters
"\n" → newline "\t" → tab "\"" → double quote inside string "\\" → backslash
Length & characters
.length()
Number of characters
"hello".length() → 5
.charAt(i)
Character at index i
"hello".charAt(1) → 'e'
Searching
.indexOf(str)
First position of str, or -1
"hello".indexOf("ll") → 2
.contains(str)
true if str is found
"hello".contains("ell") → true
.startsWith(str)
true if starts with str
"hi".startsWith("h") → true
.endsWith(str)
true if ends with str
"hi".endsWith("i") → true
Slicing & transforming
.substring(start)
From start to end
"hello".substring(2) → "llo"
.substring(s, e)
From s up to (not including) e
"hello".substring(1,4) → "ell"
.toUpperCase()
All uppercase
"hi".toUpperCase() → "HI"
.toLowerCase()
All lowercase
"HI".toLowerCase() → "hi"
.trim()
Remove leading/trailing spaces
" hi ".trim() → "hi"
.replace(a, b)
Replace a with b (all occurrences)
"aaa".replace("a","b") → "bbb"
Index reminder (0-based!)
h e l l o 0 1 2 3 4 ← index

Java starts counting at 0. "hello".charAt(0) is 'h', not 'e'.

The big rule
// WRONG — compares memory addresses, not content if (s1 == s2) { ... } // CORRECT — compares actual characters if (s1.equals(s2)) { ... }

Always use .equals() to compare string content. == checks if they're the same object in memory.

Case-insensitive compare
"Hello".equalsIgnoreCase("hello") → true
Alphabetical order — compareTo
s1.compareTo(s2) // returns negative → s1 comes before s2 // returns 0 → s1 equals s2 // returns positive → s1 comes after s2 "apple".compareTo("banana") → negative (a < b)

Used for sorting strings alphabetically.

Checking empty
s.isEmpty() // true if length == 0 s.isBlank() // true if empty or only spaces (Java 11+) s.equals("") // same as isEmpty()
Number ↔ String
// int → String String s = String.valueOf(42); → "42" String s = Integer.toString(42); → "42" String s = "" + 42; → "42" // String → int int n = Integer.parseInt("42"); → 42 // String → double double d = Double.parseDouble("3.14"); → 3.14

Integer.parseInt() throws an exception if the string isn't a valid number.

char ↔ String
// char → String char c = 'A'; String s = String.valueOf(c); → "A" // String → char (get one character) char ch = "hello".charAt(0); → 'h'
Looping through a string
String word = "hello"; for (int i = 0; i < word.length(); i++) { char ch = word.charAt(i); System.out.println(ch); }

Classic Grade 11 pattern — loop with index, grab each char with .charAt(i).

String.format (printf-style)
String s = String.format("Name: %s, Age: %d", name, age); // %s = String, %d = int, %f = double, %.2f = 2 decimal places
Live string explorer