Chapter16d.java
Select all
/* Chater16d.java CIS 260 David Klick 2016-02-15 This program demonstrates the StringBuilder class. String is great for simple tasks, and it has the advantage of having immutable objects and built-in language support such as String literals and the concatenation operator. StringBuilder is much more efficient than String if you want to modify String objects since you won't be constantly creating and disposing of objects, but rather modifying an existing object. You no longer have built-in language support, such as a concatenation operator or StringBuilder literals. You also have to convert StringBuilder objects to String objects (toString) if you want to use them for printing, etc. StringBuilder objects are NOT thread safe. StringBuffer objects are similar to StringBuilder, but thread safe. That also means that StringBuffer objects are much slower than Stringbuilder objects. */ public class Chapter16d { public static void main(String[] args) { // StringBuffer is similar, but thread safe (and therefore much slower) StringBuilder sb = new StringBuilder(); for (int i=0; i<=9; i++) sb.append(i); for (char c='a'; c<='z'; c++) sb.append(c); for (char c='A'; c<='Z'; c++) sb.append(c); System.out.println(sb.toString()); } }