OnJava8-Examples/references/ImmutableStrings.java

22 lines
548 B
Java
Raw Normal View History

2015-05-05 11:20:13 -07:00
//: references/ImmutableStrings.java
// Demonstrating StringBuffer.
public class ImmutableStrings {
public static void main(String[] args) {
String foo = "foo";
2015-05-18 23:05:20 -07:00
String s = "abc" + foo + "def"
+ Integer.toString(47);
2015-05-05 11:20:13 -07:00
System.out.println(s);
// The "equivalent" using StringBuffer:
StringBuffer sb =
2015-05-18 23:05:20 -07:00
new StringBuffer("abc"); // Creates String
2015-05-05 11:20:13 -07:00
sb.append(foo);
2015-05-18 23:05:20 -07:00
sb.append("def"); // Creates String
2015-05-05 11:20:13 -07:00
sb.append(Integer.toString(47));
System.out.println(sb);
}
2015-05-18 23:05:20 -07:00
} /* Output:
abcfoodef47
abcfoodef47
*///:~