-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCapacityOfMutableStrings.java
41 lines (31 loc) · 1.23 KB
/
CapacityOfMutableStrings.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class CapacityOfMutableStrings {
public static void main(String[] args) {
/* Example 1
StringBuilder sb = new StringBuilder();
System.out.println(sb.capacity()); // default capacity 16
sb.append("abcdefghijklmnop");
System.out.println(sb.capacity()); //16
sb.append("q");
System.out.println(sb.capacity()); //34
sb.append("r");
System.out.println(sb.capacity()); // How many character you can add (34)
System.out.println(sb.length()); // How many character is present (18)
*/
/* Example 2
StringBuffer sb2 = new StringBuffer("sachin");
System.out.println(sb2.capacity()); // 16+6 = 22
System.out.println(sb2.charAt(1));
sb2.setCharAt(1,'A'); //sAchin
System.out.println(sb2);
*/
/* Example 3
StringBuilder sb = new StringBuilder(150);
System.out.println(sb.capacity()); //150
sb.append("Hello");
System.out.println(sb); // Hello
sb.trimToSize();
System.out.println(sb.capacity()); // 5
System.out.println(sb.reverse()); //olleH
*/
}
}