Skip to content

Commit a7c3c97

Browse files
authored
Update Strings_Introduction.md
1 parent bad26bd commit a7c3c97

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/com/deepak/data/structures/Strings/Strings_Introduction.md

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
## Strings Introduction
22

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+
```
610

711
```java
812
String s1 = "Hello"; // New string creation using literals.
@@ -12,23 +16,37 @@ System.out.println(s1 == s2); // Prints True
1216
System.out.println(s1 == s3); // Prints False
1317
```
1418

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+
1623
```java
1724
String s3 = new String("Hello").intern();
1825
```
1926
<img width="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">
2027

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+
```
2233

2334
**StringBuffer and StringBuilder :**
35+
36+
```
2437
- 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.
2538
- 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.
2639
- 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+
```
2741

2842
**StringTokenizer**
43+
```
2944
- Very often, you need to break a line of texts into tokens delimited by white spaces. The java.util.StringTokenizer class supports this.
45+
```
3046

3147
**String API's :**
48+
49+
```
3250
- Below are some basic API's. All are not covered here,
3351
- **charAt()**
3452
- 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,
5068
- 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
5169
- **trim()**
5270
- returns a string from which any leading and trailing white spaces has been removed
71+
```

0 commit comments

Comments
 (0)