-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringAndBufferDemo.java
More file actions
63 lines (46 loc) · 1.69 KB
/
StringAndBufferDemo.java
File metadata and controls
63 lines (46 loc) · 1.69 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* String - immutable,
*
* StringBuffer - mutable, Synchronized & thread-safety
*
* StringBuilder - mutable, non Synchronized
*/
public class StringAndBufferDemo {
public static void main(String args[])
{
//constant pool
String str1 = "Hello";
String str = new String("Hello");
//String is immutable i..e once created, String object content
//cannot be changed, where as StringBuffer is mutable
String str2 = "Hello";
System.out.println("Original String:"+str);
str = str.concat("ggggg"); //Hellogggggg
//str doesn't get updated itself, rather concatenated string is returned
//old str object with "first" value remains unchanged and continues to exist,
//even after new concatinated string "first ggggg" gets created
System.out.println("After Concatinating String:"+str);
str = str.replaceAll("f", "m");
System.out.println("After replacing String:"+str);
//get single character in a String
char ch = str.charAt(2); //2 is index of required character
//System.exit(0);
StringBuffer strb = new StringBuffer("second");
//StringBuffer is mutable, and can grows and shrink in size dynamically
ch = strb.charAt(3);
System.out.println("Original StringBuffer:"+strb);
strb.reverse();
System.out.println("Reversed StringBuffer:"+strb);
strb.append(" tttttt");
System.out.println(strb);
strb.insert(6, "bbbbbbbbb");
System.out.println(strb);
strb.delete(3, 7);
System.out.println(strb);
//not synchronized
StringBuilder sb = new StringBuilder("Testing purpose");
sb.reverse();
//synchronized
StringBuffer sbu = new StringBuffer("fdshfiusd");
}
}