QuickDevGuide

Home › Java

Strings & StringBuilder — Java

String is immutable — every "modification" creates a new object. Literals are stored in the string pool, so identical literals share one instance. Comparing strings Always use equals() (or equalsIgnoreCase()) — == compares references and only sometimes works because of pooling. Put the literal first ("yes".equals(input)) to avoid NullPointerException. Most-used methods Task Method Length / char length(), charAt(i), chars() Search contains, indexOf, startsWith, endsWith, matches(regex) Extract substring(begin, end), split(regex) Transform toUpperCase, trim / strip (Unicode-aware), replace, repeat(n) Check isEmpty(), isBlank() (Java 11+) Join String.join(", ", list), Collectors.joining StringBuilder Concatenating in a loop with + creates a new String each iteration (O(n²)). Use…

Key Takeaways