Skip to main content
String, StringBuffer, and StringBuilder
- String objects are immutable, and String reference variables are not.
- If you create a new String without assigning it, it will be lost to your program.
- If you redirect a String reference to a new String, the old String can be lost.
- String methods use zero-based indexes, except for the second argument of substring().
- The String class is final—its methods can't be overridden.
- When the JVM finds a String literal, it is added to the String literal pool.
- Strings have a method: length(); arrays have an attribute named length.
- The StringBuffer's API is the same as the new StringBuilder's API,
except that StringBuilder's methods are not synchronized for thread
safety.
- StringBuilder methods should run faster than StringBuffer methods.
- All of the following bullets apply to both StringBuffer and StringBuilder:
- They are mutable—they can change without creating a new object.
- StringBuffer methods act on the invoking object, and objects can change without an explicit assignment in the statement.
- StringBuffer equals() is not overridden; it doesn't compare values.
- Remember that chained methods are evaluated from left to right.
- String methods to remember: charAt(), concat(), equalsIgnoreCase(),
length(), replace(), substring(), toLowerCase(), toString(),
toUpperCase(), and trim().
- StringBuffer methods to remember: append(), delete(), insert(), reverse(), and toString().