You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/com/deepak/data/structures/Strings/Strings_Introduction.md
+24-5Lines changed: 24 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,12 @@
1
1
## Strings Introduction
2
2
3
-
Strings are basically sequence of characters stored in a memory. They are immutable in java i.e you cannot change the string object itself but you can change the reference of the object. Java provides a String class to create a manipulate strings. These are objects backed up by char array.
4
-
5
-
When a string is created, it's value is cached in string pool and then stored in Heap. Below is the example of creation of string using, both literal and object. When it's created using literals, it's value is cached but when created using the new object, it does not get's cached, until we explicitly asks to cache it.
3
+
```
4
+
- Strings are basically sequence of characters stored in a memory.
5
+
- They are immutable in java i.e you cannot change the string object itself but you can change the reference of the object.
6
+
- Java provides a String class to create a manipulate strings. These are objects backed up by char array.
7
+
- When a string is created, it's value is cached in string pool and then stored in Heap.
8
+
- Below is the example of creation of string using, both literal and object. When it's created using literals, it's value is cached but when created using the new object, it does not get's cached, until we explicitly asks to cache it.
9
+
```
6
10
7
11
```java
8
12
String s1 ="Hello"; // New string creation using literals.
@@ -12,23 +16,37 @@ System.out.println(s1 == s2); // Prints True
12
16
System.out.println(s1 == s3); // Prints False
13
17
```
14
18
15
-
To make s2 and s3 point to same reference, we have to invoke the API **intern()** of String class. This API will ensure that string gets cached in the string pool.
19
+
```
20
+
- To make s2 and s3 point to same reference, we have to invoke the API **intern()** of String class. This API will ensure that string gets cached in the string pool.
21
+
```
22
+
16
23
```java
17
24
String s3 =newString("Hello").intern();
18
25
```
19
26
<imgwidth="614"alt="screen shot 2017-02-12 at 11 51 43 am"src="https://cloud.githubusercontent.com/assets/3439029/22865449/c407fafa-f119-11e6-89ca-04d45abe425b.png">
20
27
21
-
Strings are marked as final in java, i.e once a value is assigned to the string, it cannot change. For ex., the method toUpperCase() constructs and returns a new String instead of modifying the its existing content. Moreover, Strings are thread safe as well in java.
28
+
```
29
+
- Strings are marked as final in java, i.e once a value is assigned to the string, it cannot change.
30
+
- For ex., the method toUpperCase() constructs and returns a new String instead of modifying the its existing content.
31
+
- Moreover, Strings are thread safe as well in java.
32
+
```
22
33
23
34
**StringBuffer and StringBuilder :**
35
+
36
+
```
24
37
- As explained earlier, Strings are immutable because String literals with same content share the same storage in the string common pool. Modifying the content of one String directly may cause adverse side-effects to other Strings sharing the same storage.
25
38
- JDK provides two classes to support mutable strings: StringBuffer and StringBuilder (in core package java.lang) . A StringBuffer or StringBuilder object is just like any ordinary object, which are stored in the heap and not shared, and therefore, can be modified without causing adverse side-effect to other objects.
26
39
- StringBuilder class was introduced in JDK 1.5. It is the same as StringBuffer class, except that StringBuilder is not synchronized for multi-thread operations. However, for single-thread program, StringBuilder, without the synchronization overhead, is more efficient.
40
+
```
27
41
28
42
**StringTokenizer**
43
+
```
29
44
- Very often, you need to break a line of texts into tokens delimited by white spaces. The java.util.StringTokenizer class supports this.
45
+
```
30
46
31
47
**String API's :**
48
+
49
+
```
32
50
- Below are some basic API's. All are not covered here,
33
51
- **charAt()**
34
52
- returns the character located at the specified index.
@@ -50,3 +68,4 @@ Strings are marked as final in java, i.e once a value is assigned to the string,
50
68
- returns the string representation of the object used to invoke this method. toString() is used to represent any Java Object into a meaningful string representation
51
69
- **trim()**
52
70
- returns a string from which any leading and trailing white spaces has been removed
0 commit comments