Possible Duplicate:
String, StringBuffer, and StringBuilder
what is the difference (advantages, disadvantages) between using StringBuilder instead String
StringBuilder text = new StringBuilder();
String cadena = "";
Scanner scanner = new Scanner(new FileInputStream(fFileName), fEncoding);
try {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
text.append(line);
cadena += line;
}
} finally {
scanner.close();
}
It’s faster, but isn’t thread-safe.
You can build strings basically three ways.
Some other distinctions:
String: Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Concatenating strings using the
+operator doesn’t modify the Strings involved, it creates a new String that is a combination of the Strings you’re concatenating.StringBuffer: A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified.
StringBuilder: A mutable sequence of characters. This class provides an API compatible with StringBuffer, but with no guarantee of synchronization.