Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 106aa94

Browse files
Update Strings_Introduction.md
1 parent a7c3c97 commit 106aa94

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed
Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
## Strings Introduction
22

3-
```
43
- Strings are basically sequence of characters stored in a memory.
54
- They are immutable in java i.e you cannot change the string object itself but you can change the reference of the object.
65
- Java provides a String class to create a manipulate strings. These are objects backed up by char array.
76
- When a string is created, it's value is cached in string pool and then stored in Heap.
87
- 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-
```
108

119
```java
1210
String s1 = "Hello"; // New string creation using literals.
@@ -16,56 +14,50 @@ System.out.println(s1 == s2); // Prints True
1614
System.out.println(s1 == s3); // Prints False
1715
```
1816

19-
```
2017
- 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-
```
2218

2319
```java
2420
String s3 = new String("Hello").intern();
2521
```
2622
<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">
2723

28-
```
2924
- Strings are marked as final in java, i.e once a value is assigned to the string, it cannot change.
3025
- For ex., the method toUpperCase() constructs and returns a new String instead of modifying the its existing content.
3126
- Moreover, Strings are thread safe as well in java.
32-
```
3327

3428
**StringBuffer and StringBuilder :**
3529

36-
```
3730
- 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.
3831
- 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.
3932
- 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-
```
4133

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

4738
**String API's :**
4839

49-
```
40+
```java
5041
- Below are some basic API's. All are not covered here,
51-
- **charAt()**
42+
- charAt()
5243
- returns the character located at the specified index.
53-
- **equalsIgnoreCase()**
44+
- equalsIgnoreCase()
5445
- determines the equality of two Strings, ignoring their case (upper or lower case doesn't matters with this function.
55-
- **length()**
46+
- length()
5647
- returns the number of characters in a String.
57-
- **replace()**
48+
- replace()
5849
- replaces occurrences of character with a specified new character.
59-
- **substring()**
50+
- substring()
6051
- returns a part of the string. This method has two forms,
6152
- public String substring(int begin);
6253
- public String substring(int begin, int end);
63-
- **toLowerCase()**
54+
- toLowerCase()
6455
- returns string with all uppercase characters converted to lowercase
65-
- **valueOf()**
56+
- valueOf()
6657
- 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 Java Object into a meaningful string representation
61+
- trim()
7062
- returns a string from which any leading and trailing white spaces has been removed
7163
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /