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
- Strings are basically sequence of characters stored in a memory.
5
4
- They are immutable in java i.e you cannot change the string object itself but you can change the reference of the object.
6
5
- Java provides a String class to create a manipulate strings. These are objects backed up by char array.
7
6
- When a string is created, it's value is cached in string pool and then stored in Heap.
8
7
- 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
-
```
10
8
11
9
```java
12
10
String s1 ="Hello"; // New string creation using literals.
@@ -16,56 +14,50 @@ System.out.println(s1 == s2); // Prints True
16
14
System.out.println(s1 == s3); // Prints False
17
15
```
18
16
19
-
```
20
17
- 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
18
23
19
```java
24
20
String s3 =newString("Hello").intern();
25
21
```
26
22
<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">
27
23
28
-
```
29
24
- Strings are marked as final in java, i.e once a value is assigned to the string, it cannot change.
30
25
- For ex., the method toUpperCase() constructs and returns a new String instead of modifying the its existing content.
31
26
- Moreover, Strings are thread safe as well in java.
32
-
```
33
27
34
28
**StringBuffer and StringBuilder :**
35
29
36
-
```
37
30
- 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.
38
31
- 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.
39
32
- 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
-
```
41
33
42
34
**StringTokenizer**
43
-
```
35
+
44
36
- Very often, you need to break a line of texts into tokens delimited by white spaces. The java.util.StringTokenizer class supports this.
45
-
```
46
37
47
38
**String API's :**
48
39
49
-
```
40
+
```java
50
41
-Below are some basic API's. All are not covered here,
51
-
- **charAt()**
42
+
- charAt()
52
43
- returns the character located at the specified index.
53
-
- **equalsIgnoreCase()**
44
+
- equalsIgnoreCase()
54
45
- determines the equality of two Strings, ignoring their case (upper or lower case doesn't matters with this function.
55
-
- **length()**
46
+
- length()
56
47
- returns the number of characters in a String.
57
-
- **replace()**
48
+
- replace()
58
49
- replaces occurrences of character with a specified new character.
59
-
- **substring()**
50
+
- substring()
60
51
- returns a part of the string. This method has two forms,
61
52
-publicString substring(int begin);
62
53
-publicString substring(int begin, int end);
63
-
- **toLowerCase()**
54
+
- toLowerCase()
64
55
- returns string with all uppercase characters converted to lowercase
65
-
- **valueOf()**
56
+
- valueOf()
66
57
- used to convert primitive data types into Strings.
67
-
- **toString()**
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
69
-
- **trim()**
58
+
- toString()
59
+
- returns the string representation of the object used to invoke this method.
60
+
- toString() is used to represent any JavaObject into a meaningful string representation
61
+
- trim()
70
62
- returns a string from which any leading and trailing white spaces has been removed
0 commit comments